MongoDB – Multikey Indexes

In MongoDB we can have a key that holds an array  like

We can have an index on interests key. like (suppose we  have a people collection and interests field in it)

MongoDB will create an index for every item in the  array like for swimming, reading, programming. If we do a query  to find swimming the it will be fast because it is able to use index and  is in array called multikey index.

We can create a compound index from say two fields like {interests, color}.  That works fine but MongoDB will restrict if both keys are arrays an index is a multikey index. While inserting in to these field MongoDB will not allow us to insert.

suppose we insert a document into a collection.

We can create an index  like db.foo.ensureIndex({a:1, b:1}); . This will work just fine. If we insert like

or

. It will work as expected but if we insert both fields as an array

Db will not allow  us and we will get an error saying.  “Cannot index parallel arrays b[] a[]”

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.