How to Create a nested index in MongoDB?
Asked Answered
D

4

70

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.

Declination answered 16/3, 2012 at 0:21 Comment(0)
S
117

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.

Smut answered 16/3, 2012 at 0:30 Comment(0)
A
15

MongoDB automatically creates a multikey index if any indexed field is an array; you do not need to explicitly specify the multikey type.

This will work for both the scenario's

db.coll.createIndex( { "addr.pin": 1 } )

Scenario 1 nested OBJECTS

{
  userid: "1234",
  addr: {
    pin:"455522"
  }
},
{
  userid: "1234",
  addr: {
    pin:"777777"
  }
}

Scenario 2 nested Arrays

{
  userid: "1234",
  addr: [
    { pin:"455522" },
    { pin:"777777" },
  ]
}

https://docs.mongodb.com/manual/core/index-multikey/

Ayn answered 11/5, 2017 at 10:53 Comment(0)
W
13

A. to index all the properties in "nested" you will have to index them separately:

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

This can be done in one command with:

db.collection.createIndexes([{"attrs.nested.value": 1}, {"attrs.nested.valuetwo": 1}]);

B. to index just "valuetwo":

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

Use createIndex over ensureIndex as ensureIndex is Deprecated since version 3.0.0

Wetzel answered 29/2, 2016 at 1:47 Comment(3)
The two sets of index creation commands you give in A are not equivalent. The first creates separate indexes on value and valuetwo. The second creates a single compound index on both of these values. This index can only be used for queries which specify a value for value. That is, it's not useful for queries on valuetwo alone.Casuistry
There is a createIndexes() command (plural) which can create multiple indexes in one command. (As @MichaelMior points out, your code creates one compound index, not two separate indexes.) I haven't tried it, but it looks like you would do- db.collection.createIndexes([{"attrs.nested.value": 1}, {"attrs.nested.valuetwo": 1}]);Ancipital
@Ancipital Thanks for pointing out createIndexes. I edited the answer to use this.Casuistry
H
2

It looks like there may have been a new feature to address this since these answers have been written.

Mongodb has wildcard indexes that allow you to create an index for a field that contains varying keys.

From the documentation:

If the field is a nested document or array, the wildcard index recurses into the document/array and indexes all scalar fields in the document/array.

So the index in the question would be constructed like this:

db.collections.createIndex({ "attrs.nested.$**": 1 })

You can read more about this here!

The previous answers nearly gave me a heart attack.

Hargeisa answered 4/10, 2022 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.