1

I have Document

class Store(Document):
    store_id = IntField(required=True)
    items = ListField(ReferenceField(Item, required=True))
    meta = {
        'indexes': [
            {
                'fields': ['campaign_id'],
                'unique': True
            },
            {
               'fields': ['items']
            }
        ]
    }

And want to set up indexes in items and store_id, does my configuration right?

1 Answer 1

2

Your second index declaration looks like it should do what you want. But to make sure that the index is really effective, you should use explain. Connect to your database with the mongo shell and perform a find-query which should use that index followed by .explain(). Example:

db.yourCollection.find({items:"someItem"}).explain();

The output will be a document with lots of fields. The documentation explains what exactly each field means. Pay special attention to these fields:

  • millis Time in milliseconds the query required
  • indexOnly (self-explaining)
  • n number of returned documents
  • nscannedObjects the number of objects which had to be examined without using an index. For an index-only query this should be equal to n. When it is higher, it means that some documents could not be excluded by an index and had to be scanned manually.
Sign up to request clarification or add additional context in comments.

1 Comment

indexOnly isn't really self-explaining: it indicates a covered query. In this special case all of the fields in the query and all of the fields in the result are part of the same index.

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.