0

I have some documents in my mongodb database with the following structure, I want to know how to create an index for document.data.0.data[0-9999].val1?

My problem is where I type [0-9999] I don't know how many elements will be inside this data array, but I want that all the elements inside this array have an index on the val1 key.

Any tips?

{
  "_id" : ObjectId("55c455c2fc07853b78c4b6b9"),     
  "data": {
    "0": {
      "data": [
      {
        val1: "abc",
        val2: "def"
      },
      {
        val1: "abc",
        val2: "def"
      }
      ]
    }
  }
}

1 Answer 1

1

You don't include the array index values in the index, so it would just be:

db.mycoll.ensureIndex({'data.0.data.val1': 1})

This is termed a multikey index in MongoDB.

Sign up to request clarification or add additional context in comments.

1 Comment

I will try. It will know that it must create the index for all the elements?

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.