When find and sort are used together, how does mongodb handle it?

  mongodb, question

Today atMongodb index help documentFound in the following description:
Collection order has the following index:

{ qty: 1 }
 { status: 1, ord_date: -1 }
 { status: 1 }
 { ord_date: -1 }

Query statement:

db.orders.find( { qty: { $gt: 10 } } ).sort( { status: 1 } )

That is, MongoDB does not use the { qty: 1 } index for the query, and the separate { status: 1 } or the { status: 1, ord_date: -1 } index for the sort.

Sort cannot use {status: 1} or {status: 1, ord_date: -1} I understand, but why can’t find use the index {qty: 1}?

Add: I understand it wrong, the query finally used the index {qty:1}

First, let me ask you a question. Have you ever tried which index is used in this query?
Again, I think you probably misunderstood the meaning of the English passage. The query engine will not be used{qty:1}To satisfyfindTheAt the same timeReuse{status: 1}Or{status: 1, ord_date: -1}To satisfysort. Let me put it in a more clear way: queries can either be used{qty: 1}To satisfyfind(then use memory sorting), or use{status: 1}Or{status: 1, ord_date: -1}The sorting is satisfied (but only the result set can be found by collection scanning).
There is no index in your existing index that can satisfy both query and sorting. To achieve this goal:{status: 1, qty: 1}. But the problem is that this is not necessarily a good choice. Maybe{qty: 1}+Memory sorting is more efficient. It all depends on your data distribution. The specific principle is actually the same as index in RDBMS, or index using B/B+ tree. I don’t know a few words. I’d better check the relevant information.
The simple way is to build all the indexes mentioned just now, then run the query and let them tear themselves up. Finally, the query engine will select the best index for you based on the sampled data.
So back to the original question, did you try which index you chose? Because the final index winner is determined based on the distribution of your data, there is no definite answer here. I am willing to gamble{status: 1, qty: 1}Win.