如何在电机中创建2dsphere索引。

时间:2016-04-14 19:24:59

标签: python mongodb tornado-motor

我尝试编写地理空间查询。 我使用motor连接到MongoDB。这是我如何插入数据。

result = yield db['users'].find_and_modify(query={"email": data['email'], "password": data['password']},
                                               update={"$set": {
                                                   "location": {"coordinates": {"longitude": data['longitude'],
                                                                                "latitude": data['latitude']}}}},
                                               upsert=True)

但我不能为这个字段创建索引。 我试试:

 yield db.users.create_index({"location": "2dsphere"})

但我有错误

  

TypeError:如果未指定方向,则key_or_list必须是list的实例   错误:tornado.access:500 GET

1 个答案:

答案 0 :(得分:2)

与PyMongo相同,加上“yield”:

yield db.users.create_index([("location", "2dsphere")])

创建索引时,MongoDB的Python驱动程序需要一个元组列表,而不是一个dict,因为键顺序和dicts不保留键顺序。