Reputation: 705
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?
Upvotes: 0
Views: 1076
Reputation: 34149
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.
Upvotes: 1