Creating Indexes - MongoDB

My โ€œtableโ€ looks like this:

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

Is there a way to index items in an action list? Or do I need to split them into further tables?

It would be much more convenient for me to store actions in the current row of the table.

+3
source share
2 answers

Thanks to skot at #mongodb !!

One solution:

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

to create an index in the time field in the action list.

+8
source

Example for pymongo:

import pymongo

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

Source: https://habr.com/ru/post/1756336/


All Articles