I can not understand how indexes work in MongoDB?

Study indexing in MongoDB, there appeared the following questions:
1) If there are two conflicting index of how Mongo determines which index to sample?
For example, suppose you have a collection with documents like {'name': 'VasyaPupkin', 'age': 30}. There are two index {'name': 1, 'age': -1} and {'name': -1, 'age': -1}. Make the sample db.users.find({"name": "???"}). What index will be used?

2) how Important is the order of the keys in a composite index?

3) If the relationship between the indexes and specifying the sort (find(...).sort(...)) in the query?
June 8th 19 at 16:50
2 answers
June 8th 19 at 16:52
Solution
1) "Contrary to" indexes will not. Possible, for example, is redundant.
{'name': 1, 'age': -1}
and
{'name': -1, 'age': -1}
it's two different index.
On the intricacies of the query optimizer, I will not say, but monga, I'm sure, smart enough to use the index, which is needed at the moment. Here here, beautifully painted as monga analyzes the queries.

2) of Course Yes. If we make the index
name, age
it is in this order we must submit items in the query, otherwise the index will be useless.

3) If I understand correctly the question, the index is needed for search and sorting.
1) I understand that this is two different index but it is not clear what will be used in the case of a request say find({"name": "Petya", "age": 17}). Ie if Mongo when you select the index focuses on the fields of the request, it is unclear which of the above will be selected.

2) Why? Unless the query keys have search priority? I suggest that Mongo first searches for all the documents on the first field specified in the query, and then makes the obtained set of docks, a new set already on the basis of the second field. Correct if it is not.

3) Well for example when I understood, I have made N number of documents and which index sorted by name. Made the request, I returned valid docks, but not sorted by index. So I still have to sort, I must then call the sort method. So now I have a question, the index in the call to sort is used (helps him sort) or it is needed only to search for documents? - Benjamin.Keebler commented on June 8th 19 at 16:55
did you bother to read the link about analyzing queries and using indexes? - Shakira.Cruickshank commented on June 8th 19 at 16:58
Yes , I have read, there simply is an example that sample rate affects the order of the keys, but I'm even more confused now, because there turned out faster when index fields are not in the same order they are in the query. Explain please? - Benjamin.Keebler commented on June 8th 19 at 17:01
1) to Answer reliably the question I can not. It is important that the data is sorted the way you want it. And it will only select those that you need.

2) You kind of answered your own question)
Let's build phonebook first by surname, then by age. So you will have boxes with the letters "A", "B", ..., "I". But inside all will be sorted by surname, then by age.

Then in the query you say: give me all my dear Ivanov, thirty years. Then the index will be used. We found a box with the letter And opened it and quickly did the required sample.

If you are having only the index, ask them to find all thirty, then Mongo will have to review all the records to find what you need. Of course, because you possibly does not systematized age, he is considered only after sorting by name.

3) the Index is used for searching and for sorting. If you use it (search and sort), of course. - Shakira.Cruickshank commented on June 8th 19 at 17:04
thanks , became much clearer! - Benjamin.Keebler commented on June 8th 19 at 17:07
June 8th 19 at 16:54
Solution
Thank you, very nice article! - Benjamin.Keebler commented on June 8th 19 at 16:57

Find more questions by tags MongoDB