2010-07-24 75 views
3

我的“表”是这样的:创建索引 - MongoDB的

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

有没有什么办法可以索引操作列表中的项目?还是我不得不将它们分成更多的表格?

对我来说,将行为保持在当前表格行中会更加方便。

回答

8

感谢skot in #mongodb !!

一种解决方案是:

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

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

+1

给定码是JavaScript的,实际的方法是 'ensure_index' 在Python – romanlv 2014-06-09 04:56:35

12

示例pymongo:

import pymongo 

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

如果要删除的重复项:'collection.ensure_index( 'HOST_NAME',唯一=真, drop_dups =真)' – bahmait 2014-06-27 16:24:49