I'm a little confused by your question, simply because the index you specified ( { first_name: 1, archived: 1 } ) is a composite index. All of the following queries will use this composite index:
conditions = { archived: false, first_name: "Billy" }; conditions = { first_name: "Billy", archived: false }; conditions = { first_name: "Billy" };
Now suppose we have two separate indexes: { first_name: 1 } and { archived: 1 } . In this case, MongoDB will perform query optimization to determine which index is most efficient to use. You can learn more about MongoDB's query optimization here.
The MongoDB query optimizer, therefore, is likely to use the same index for both of the multi-connector queries you provided:
conditions = { archived: false, first_name: "Billy" }; conditions = { first_name: "Billy", archived: false };
Alternatively, you can use hint to force MongoDB to use the index of your choice. All in all, this is probably not a good idea . You can also manually check which index is most effective for a particular query as described here .
You can see which index is used in the query using the .explain() function in the Mongo shell. (If the index is not used, you will see "cursor" : "BasicCursor" in the resulting document. On the other hand, if you use a compound index, you will see something like "cursor" : "BtreeCursor first_name_1_archived_1" . If one of the indices is one field, you can see "cursor" : "BtreeCursor archived_1" .
In addition, the search strategy for MongoDB works as follows:
- first, cross the index using the boundaries of the index to filter out as many documents as possible;
- next, if there are additional predicates that cannot be executed using the index,
- document extraction
- apply predicate
- and include / exclude the document from the results.
The query optimizer runs all possible query plans in parallel and selects the "best", however, all query plans follow the strategy above. (The BasicCursor is a degenerate case: it goes through all the documents and applies the predicate to each of them.)
tl; dr? Matches are smart enough to match equality predicates when they are presented in any order.
It makes sense?