Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a nested index in MongoDB?

A. How do I index nested and all of it's values?

B. How do I index valuetwo?

{     id: 00000,     attrs: {         nested:{             value: value1,             valuetwo: value2,         }     } } 

I've looked here: http://www.mongodb.org/display/DOCS/Indexes, and the docs to my knowledge, aren't clear about indexing things that aren't nested.

like image 529
Mr. Demetrius Michael Avatar asked Mar 16 '12 00:03

Mr. Demetrius Michael


People also ask

How do I create an indexed nested object in MongoDB?

MongoDB automatically creates a multikey index if any indexed field is an array; you do not need to explicitly specify the multikey type. Show activity on this post. The two sets of index creation commands you give in A are not equivalent. The first creates separate indexes on value and valuetwo .

Can we create nested collection in MongoDB?

In MongoDB, you can only nest document up to 100 levels. The overall document size must not exceed 16 MB.

Can MongoDB use multiple indexes?

MongoDB can use the intersection of multiple indexes to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.

Can we create index on array in MongoDB?

Yes, you can and it works: To index a field that holds an array value, MongoDB creates an index key for each element in the array. These multikey indexes support efficient queries against array fields.


1 Answers

You'd create them just as if you were creating an index on a top level field:

db.collection.createIndex({"attrs.nested.value": 1}) 

You do need to explicitly create indexes on each field.

like image 170
Bryan Migliorisi Avatar answered Sep 23 '22 17:09

Bryan Migliorisi