创建索引-MongoDB

2024-05-16 01:45:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我的“桌子”是这样的:

{'name':'Rupert', 'type':'Unicorn', 'actions':[
    {'time':0, 'position':[0,0], 'action':'run'},
    {'time':50, 'position':[50,0], 'action':'stoprun'},
    {'time':50, 'position':[50,0], 'action':'jump'},
    {'time':55, 'position':[50,0], 'action':'laugh'},
    ...
]}

有什么方法可以索引操作列表中的项吗?或者我要把它们分成更多的桌子吗?

对我来说,将操作保留在当前表行中会方便得多。


Tags: 方法runnameactions列表timetypeposition
2条回答

感谢mongodb中的skot!!

一个解决方案是:

[...].ensureIndex({"actions.time":1})

用于在操作列表中的时间字段上创建索引。

pymongo示例:

import pymongo

mongo = pymongo.Connection('localhost')
collection = mongo['database']['hosts']
collection.ensure_index('host_name', unique=True)

相关问题 更多 >