Mongodb indexes cover queries

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. Use dot notation to index fields in subdocuments. For example, consider a collection of users with documents of the following form: {_id: 1, user: {login: "tester"}} The collection has the following indexes:

{user: 1}

{"user.login": 1}

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

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

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

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

However, the query uses the index {"user.login": 1} to find matching documents.

I want to know the reason why the index {"user.login": 1} does not apply to the request.

thanks

+4
source share
2 answers

The β€œmain reason” is that this feature is not currently implemented. In particular, the SERVER-2104 function, and as soon as it is implemented, you will get the desired result (so vote for it and see it). At the same time, to use closed index queries, you need to avoid using subdocuments in the index.

+4
source

"I want to know the reason why the index {" user.login ": 1} does not apply to the request.

Sorry for the late reply. Here is my explanation: All queries return the "_id" field by default, unless you omit them in the projection. Even if you did not specify "_id" in {"user.login": 1}, it will return "_id" that the reason index will not be closed.

+4
source

Source: https://habr.com/ru/post/1480475/


All Articles