创建索引 - MongoDB

3

我的“表格”长这样:

{'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'},
    ...
]}

我能否对操作列表中的项目建立索引?或者我必须将它们拆分为更多的表格?

将操作保留在当前表格行中会更加方便。

2个回答

12

pymongo的示例:

import pymongo

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

如果您想删除重复项:collection.ensure_index('host_name', unique=True, drop_dups=True) - scharfmn

8

感谢在#mongodb中的skot!!

其中一种解决方案是:

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

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

1
给定的代码是JavaScript,而在Python中实际方法是“ensure_index”。 - romanlv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接