MongoDB MongoEngine index declaration

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?

Upvotes: 1

Views: 2347

Answers (1)

Philipp
Philipp

Reputation: 69663

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.

Upvotes: 2

Related Questions