您可以在空的或不存在的 MongoDB 集合上创建索引,该索引出现在index_information
:
>>> from tornado import ioloop, gen
>>> import motor
>>>
>>> con = motor.MotorClient()
>>> db = con.test
>>> col = db.collection
>>>
>>>
>>> @gen.coroutine
... def coro():
... yield db.drop_collection("collection")
... yield col.create_index("c")
... yield col.create_index("h")
... print((yield col.index_information()))
...
>>> ioloop.IOLoop.current().run_sync(coro)
{u'c_1': {u'key': [(u'c', 1)], u'v': 1}, u'_id_': {u'key': [(u'_id', 1)], u'v': 1}, u'h_1': {u'key': [(u'h', 1)], u'v': 1}}
由于我在您的示例代码中没有看到任何“yield”语句或任何回调,我怀疑您没有正确使用 Motor。电机是异步的;为了等待与数据库服务器对话的任何 Motor 方法完成,您必须将回调传递给该方法,或者产生该方法返回的 Future。
有关更多信息,请参阅教程:
http://motor.readthedocs.org/en/stable/tutorial.html#inserting-a-document
关于使用 Motor 调用异步方法的讨论(这适用于所有 Tornado 库,而不仅仅是 Motor)从“插入文档”部分开始。