mongodb geoNear +额外的过滤器?

发布于 2024-10-29 18:16:13 字数 248 浏览 6 评论 0原文

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

{
 _id: {},
 cid: 1,
 latlon: [ -74.07096147537231, 40.9088747684256 ]
}

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

谢谢!

Is it possible to add more filters when using geoNear in mongodb? For example, say I my records look like the following:

{
 _id: {},
 cid: 1,
 latlon: [ -74.07096147537231, 40.9088747684256 ]
}

Can I pass "cid" to be sure that only the records with a "cid" that equals "1"? If it is not possible with geoNear, how would I do this? I am using geoNear because it returns the distance...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-11-05 18:16:14

是的,当然有可能。您可以像往常一样使用 $near 进行任何过滤:

db.places.find( { latlon: { $near : [50,50] } }, {cid: 1} )

更新:

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

来自文档

geoNear 命令添加了
返回距离的好处
从指定点开始的每个项目
结果以及一些
用于故障排除的诊断。

db.runCommand中有一个query参数,你可以这样使用它:

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

Yes, sure it's possible. You can use any filtering with $near as usual:

db.places.find( { latlon: { $near : [50,50] } }, {cid: 1} )

Update:

If you need distance use db.runCommand, if no need distance -- db.collection.find as usual.

From documentation:

The geoNear command has the added
benefit of returning the distance of
each item from the specified point in
the results, as well as some
diagnostics for troubleshooting.

There is query parameter in db.runCommand, you can use it like this:

db.runCommand( { geoNear : "latlon" , near : [ 50 , 50 ], num : 10,
                 query : { cid: 1 } } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文