0

Consider this object:

{
    "_id" : ObjectId("5a3040438c901f42d061edff"),
    "TrackingId" : {
        "FullValue" : "myRandomValue",
        "PrintValue" : "myRandomValue2",
        "BarcodeValue" : "myRandomValue3"
    }
}

What is the difference between doing the following:

db.myColl.createIndex( { TrackingId: 1 }} )

vs

db.myColl.createIndex( { TrackingId.FullValue : 1 } )

As you can see in the first scenario I am indexing the entire object, since it is small I have no issues doing this. However I could also index the attribute within that object. My question then is, which one of these indexes would make a look up by TrackingId.FullValue more efficient and why?

2
  • Index on embedded field vs index on embedded document Commented Dec 12, 2017 at 23:29
  • @Veeram Thank you, I have read that documentation and It does not explain which one is more efficient and why it just tells you that you can index an embedded fiel to a certain size. Commented Dec 12, 2017 at 23:35

1 Answer 1

1

Based on the help document

NOTE Although the query can use the index, the result set does not include the sample document above. When performing equality matches on embedded documents, field order matters and the embedded documents must match exactly. See Query Embedded Documents for more information regarding querying on embedded documents.

It suggests that you should be careful when creating index on the whole child document, ie. { TrackingId: 1 }. The order of your query will matter and most likely will cause too much confusion.

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

Comments

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.