我在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();
});
}
});
}
});
答案 0 :(得分:0)
console.log(item.length)
也许回调不返回数组。 您应该检查值类型。
答案 1 :(得分:0)
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);
});
});