【问题标题】:node js Aggregate with geonear returns undefined带有geonear的节点js聚合返回未定义
【发布时间】:2018-08-03 01:03:21
【问题描述】:

在 nodejs 中使用聚合时,我变得不确定,对于 find() 查询它正在工作。 python中的相同geoNear查询工作正常,请帮助我

const MongoClient = require('mongodb').MongoClient;


MongoClient.connect('mongodb://localhost:27017',(err,client) => {
    if(err){
        console.log('db connection failed');
    } else  {
        var db = client.db('DB_NAME');
        db.collection('COLLECTION',(err, collectionname) => {
            if(err){
                console.log('unable to connect to collecion');
            } else {
                collectionname.aggregate([
                    { 
                        "$geoNear": {
                            "near": 
                                 {"type": "Point","coordinates":[77.102490,28.704059]},
                              "distanceField": "distancefromcust",
                              "direction": "direction",
                              "maxDistance":  1000,
                              "spherical": "true",
                              "num":100000
                         }
                    },
                    {"$group":
                      {
                      "_id":
                          {
                              "operator":"$operator",
                              "direction":"$direction"
                          },
                          "speed":{"$push":"$speed"},
                          "samples":{"$push":"$samples"},
                          "distance":{"$push":"$distancefromcust"},
                      }
                  }
                ],(err, item) => {
                    console.log(item.length);
                    client.close();
                });
            }

        });
    }

});

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:
    console.log(item.length)
    

    也许回调不返回数组。 您应该检查值类型。

    【讨论】:

    • 我正在获取对象类型。如果我打印对象它给出“AggregationCursor”
    【解决方案2】:
        router.get('/locationgeoNear', function(req,res,next){
      Model.aggregate([
           {
               $geoNear: {
                   near: {type: 'point', coordinates: [parseFloat(req.query.lat), parseFloat(req.query.lng)] },
                   "distanceField": "100000",
                   "maxDistance": 100,
                   "spherical": true
               }
           }
       ],function(err, collection, next){
           res.send(collection);
       });
    });
    
    1. 长期以来,我一直致力于获取位置数据。有了最新的更新,我找不到其他方法。这个是最好的。
    2. 如果您没有得到结果,请从最大距离开始。
    3. 充分利用 mongoose、express、node 和 mongoDB。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 2017-10-27
      • 1970-01-01
      • 2020-06-01
      • 2018-06-10
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多