创建索引 - MongoDB

Rad*_*Hex 3 python django mongodb pymongo mongoengine

我的"桌子"看起来像这样:

{'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'},
    ...
]}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以索引动作列表中的项目吗?或者我是否必须将它们分成更多的表格?

对于我来说,将动作保持在当前表行中会更方便.

jpo*_*ppe 12

pymongo的例子:

import pymongo

mongo = pymongo.Connection('localhost')
collection = mongo['database']['hosts']
collection.ensure_index('host_name', unique=True)
Run Code Online (Sandbox Code Playgroud)


Rad*_*Hex 8

感谢#mongodbskot !!

一个解决方案是:

[...].ensureIndex({"actions.time":1})
Run Code Online (Sandbox Code Playgroud)

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