mongodb geoNear +额外的过滤器?

mik*_*ike 3 mongodb mongodb-php

在mongodb中使用geoNear时是否可以添加更多过滤器?例如,说我的记录如下所示:

{
 _id: {},
 cid: 1,
 latlon: [ -74.07096147537231, 40.9088747684256 ]
}
Run Code Online (Sandbox Code Playgroud)

我可以传递"cid"以确保只有"cid"等于"1"的记录吗?如果geoNear不可能,我该怎么做?我正在使用geoNear因为它返回距离...

谢谢!

And*_*ich 11

是的,确定它是可能的.您可以像往常一样使用$ near过滤:

db.places.find( { latlon: { $near : [50,50] } }, {cid: 1} )
Run Code Online (Sandbox Code Playgroud)

更新:

如果您需要距离使用db.runCommand,如果不需要距离 - db.collection.find像往常一样.

来自文档:

geoNear命令具有额外的好处,即返回结果中指定点的每个项目的距离,以及一些故障排除诊断.

query参数db.runCommand,你可以像这样使用它:

db.runCommand( { geoNear : "latlon" , near : [ 50 , 50 ], num : 10,
                 query : { cid: 1 } } );
Run Code Online (Sandbox Code Playgroud)