我将 Mongoose
库与 Node.js
和 geoNear
聚合函数结合使用。
这是我的一段代码,它的作用就像一个魅力:
User.geoNear({ type: "Point", coordinates: [geo.lon, geo.lat] }, {
spherical: true,
maxDistance: 50,
}, function(err, results, stats) {
if (err) {
// handle err
}
}
现在我想添加基本查询选项,例如使用 mongodb $nin
选项排除文档。例如,我想搜索给定位置周围的用户(正如我在示例中所做的那样),但有不匹配某些给定 ids
的约束。
如何将这种选项添加到我的查询中?
最佳答案
您可以使用聚合框架。尝试:
db.collection.aggregate(
[
{
'$geoNear': {
'near': {
'type': 'Point',
'coordinates': [ -77.395410 , 38.967995 ]
},
'spherical': true,
'distanceField': 'dist',
'maxDistance': 5000
}
},
{
$match:{_id:{$nin: [ObjectId("5716163704ca42c7b579c7e3")]}}
}
]);
上面的查询将搜索半径 5000 米内的所有文档,并排除给定 _id 的文档。
关于javascript - Mongoose .geoNear聚合,添加基本查询选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36712676/