gpt4 book ai didi

javascript - Mongodb $within 查询

转载 作者:行者123 更新时间:2023-11-30 10:35:26 24 4
gpt4 key购买 nike

我正在尝试使用 $within 运算符在 mongodb 中进行地理空间查询。我有一个包含位置字段的集合条目:

location: {
bounds: {
south_west: { lat: XX.XXXXXX, lng: XX.XXXXX },
north_east: { lat: XX.XXXXXX, lng: XX.XXXXX }
},
center: {
lat: XX.XXXXXX,
lng: XX.XXXXXX
}
}

对于我的查询,我想获取所有位于 google.maps 对象范围内的 location.center 地点。我有这样的东西:

query = {}

var southWest = map.getBounds().getSouthWest();
southWest = [southWest.lat(), southWest.lng()];

var northEast = map.getBounds().getNorthEast();
northEast = [northEast.lat(), northEast.lng()];

query["location.center"] = { $within: { $box: [southWest, northEast] } }

但我总是得到

Exception from Meteor.flush: Error: Unrecognized key in selector: $within
at Error (<anonymous>)
at Function.LocalCollection._exprForConstraint (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:559:11)
at Function.LocalCollection._exprForOperatorTest (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:468:36)
at Function.LocalCollection._exprForKeypathPredicate (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:387:34)
at Function.LocalCollection._exprForSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:307:36)
at Function.LocalCollection._compileSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:284:24)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:71:39)
at LocalCollection.find (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:57:10)
at _.extend.find (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:155:34)
at Template.feed.feed_items (http://localhost:3000/client/modules/js/feed.js?5ed30ddbd861944b636dbe03c13cc80421258a5b:129:17)

而且我不确定如何进行。通过一个简单的 JSON.stringify(query) 我注意到查询结果像

{"location.center":{"$within":{"$box":[[39.93623526127148,-75.63687337060549],[39.984129247435064,-75.57438862939455]]}}}

无论如何我可以防止引号被包裹在 $within$box 选择器周围吗?我相信这就是抛出我的异常的地方。

最佳答案

引号不是问题,那只是有效的 JSON。问题是 minimongo 还没有实现 $within。马特·德伯格利斯 commented on Minimongo's incomplete state (2012 年 10 月 23 日):

The recent traffic on meteor-talk reminds me that minimongo comes with a healthy wish list of improvements and extensions.

[...]

  • $near, $within, and $maxDistance.

如果在具有“真实”mongodb 访问权限的服务器中执行此操作,您可能会更幸运,但我不确定如果客户端存储没有相同的功能,它是如何工作的。

Minimongo's source code is available .也许您应该尝试自己实现这个选择器并发送一个拉取请求? :)

关于javascript - Mongodb $within 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com