4
votes

There is a sample http://docs.mongodb.org/manual/tutorial/create-indexes-to-support-queries/#indexes-covered-queries

any of the indexed fields are fields in subdocuments. To index fields in subdocuments, use dot notation. For example, consider a collection users with documents of the following form: { _id: 1, user: { login: "tester" } } The collection has the following indexes:

{ user: 1 }

{ "user.login": 1 }

The { user: 1 } index covers the following query:

db.users.find( { user: { login: "tester" } }, { user: 1, _id: 0 } )

However, the { "user.login": 1 } index does not cover the following query:

db.users.find( { "user.login": "tester" }, { "user.login": 1, _id: 0 } )

The query, however, does use the { "user.login": 1 } index to find matching documents.

I want to know the root-cause why the { "user.login": 1 } index does not cover the query.

Thank you

2
It is explained right above: "An index cannot cover a query if [...] any of the indexed fields are fields in subdocuments"... - assylias
Can you show your explain plan ? - Srivatsa N
I do not know the root cause, I must be honest, but I believe that object evaluation works and dot notation does not is because of the query operators that can be performed against dot notation - Sammaye

2 Answers

4
votes

The "root cause" is that this feature is not currently implemented. Specifically, the feature is SERVER-2104 and once that is implemented you will get the result you need (so go vote for it and watch it). In the mean time, to utilize covered index queries you need to avoid the use of sub-documents in the index.

4
votes

"I want to know the root-cause why the { "user.login": 1 } index does not cover the query."

Sorry for the late reply. Here is my explanation: All queries by default returns "_id" field unless you omit them in projection. Even though you didn't mention "_id" in { "user.login": 1 }, it will return the "_id", that's the reason index is not covered.