Motorengine是一个很棒的库,可以用mongodb进行异步数据库操作。但是我想知道如何使用motorengine进行地理空间查询。因为该库不支持地理字段。我的选择是使用马达2dspear索引。但如果我能在发动机的帮助下找到一种方法,那就太好了。
有谁能帮我一下吗。
发布于 2016-08-25 02:37:50
我像这样解决了这个问题。
from motorengine.document import Document
import pymongo
class BaseDocument(Document):
import pymongo
ASCENDING = pymongo.ASCENDING
DESCENDING = pymongo.DESCENDING
GEO2D = pymongo.GEOSPHERE
def __init__(self, alias=None, **kwargs):
indexes = self.__indexes__ if hasattr(self, "__indexes__") else []
if len(indexes) == 0:
return
def ensure_index(index, **spec):
self.objects.coll(alias).ensure_index(index, **spec)
for index_spec in indexes:
ensure_index([index_spec])
super(BaseDocument, self).__init__(**kwargs)以及它的使用索引。
class Team(BaseDocument):
__indexes__ = [('location', BaseDocument.GEO2D)]
name = StringField(required=True)
location = GeoPointField()
contact = StringField(required=True)https://stackoverflow.com/questions/39091522
复制相似问题