Moongose .geoNear聚合,添加基本查询选项 [英] Moongose .geoNear aggregation, add basic query options
问题描述
我使用 Mongoose
库和 Node.js
,以及 geoNear
聚合函数。
I am using the Mongoose
library with Node.js
, and the geoNear
aggregation function.
这是我的一段代码,就像一个魅力:
Here is my piece of code, that works like a charm :
User.geoNear({ type: "Point", coordinates: [geo.lon, geo.lat] }, {
spherical: true,
maxDistance: 50,
}, function(err, results, stats) {
if (err) {
// handle err
}
}
现在我想添加基本查询选项,例如使用mongodb $ nin
选项。例如,我想搜索给定位置周围的用户(就像我在我的示例中所做的那样),但是约束不匹配某些给定的 ids
。
怎么可能在我的查询中添加这种选项?
Now I would like to add basic query options, such as excluding documents with the mongodb $nin
option. For example, I would like to search for users around a given position (as I did in my example) but with a constraint of not matching some given ids
.
How could it be possible to add this kind of options to my query ?
推荐答案
你可以做使用聚合框架。尝试:
You can do with with aggregation framework. Try:
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的文档。
Query above will search all documents with in 5000 meters radius and exclude a document with given _id.
这篇关于Moongose .geoNear聚合,添加基本查询选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!