3

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 any way I can index the items within the actions list? Or do I have to split them up into further tables?

It would be a lot more convenient for me to keep the actions within the current table row.

2 Answers 2

12

Example for pymongo:

import pymongo

mongo = pymongo.Connection('localhost')
collection = mongo['database']['hosts']
collection.ensure_index('host_name', unique=True)
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to drop the duplicates: collection.ensure_index('host_name', unique=True, drop_dups=True)
8

Thanks to skot in #mongodb!!

One solution is:

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

for creating an index on the time field within the actions list.

1 Comment

given code is javascript, the actual method is 'ensure_index' in python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.