简体   繁体   English

Pymongo Asyncmongo $ max距离不起作用

[英]Pymongo Asyncmongo $maxDistance not working

I'm trying to query MongoDB geo using $maxDistance . 我正在尝试使用$maxDistance查询MongoDB geo But its not working in both PyMongo and AsyncMongo 但它在PyMongoAsyncMongo中均AsyncMongo


AsyncMongo 异步蒙哥

self.application.asyncdb.places.find({"loc":{"$near":[longitude, latitude], "$maxDistance":1000}}, limit=20, callback=self.showNearByPlaces)


PyMongo PyMongo

placeColl = self.application.db.places
cursorObj = placeColl.find({"loc": {"$near":(longitude, latitude)},"$maxDistance":1000})

Here's an example using PyMongo 2.6.3 and MongoDB 2.4.8: 这是使用PyMongo 2.6.3和MongoDB 2.4.8的示例:

>>> from pymongo import MongoClient
>>> from bson import SON
>>> c = MongoClient()
>>> collection = c.db.collection
>>> collection.drop()
>>> collection.create_index([('loc', '2d')])
u'loc_2d'
>>> collection.insert({'loc': [1, 1]})
ObjectId('52d19a5eca1ce961b94f23f4')
>>> collection.insert({'loc': [3, 3]})
ObjectId('52d19a69ca1ce961b94f23f5')
>>> list(collection.find({'loc': SON([('$near', [0, 0]), ('$maxDistance', 2)])}))
[{u'loc': [1, 1], u'_id': ObjectId('52d19a5eca1ce961b94f23f4')}]

You can see that the point within 2 units of [0, 0] is included in the results, and the farther point is excluded. 您可以看到结果中包含[0,0] 2个单位内的点,而更远的点被排除。

Unlike most MongoDB queries, $near queries require the keys to be in a particular order. 与大多数MongoDB查询不同, $near查询要求键必须按特定顺序排列。 $near must come before $maxDistance , not after. $near必须在$maxDistance之前,而不是之后。 Since regular Python dicts don't preserve the order of the keys, I used a bson.SON , which is installed as part of the PyMongo package. 由于常规Python字典不保留键的顺序,因此我使用了bson.SON ,它是作为PyMongo软件包的一部分安装的。 SON is like a dictionary but preserves order. SON就像字典一样,但保留顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM