The find() command generally behaves as if the components of the query are performed in the following order:
match -> sort -> skip -> limit -> project
This can lead to very confusing behavior, given that users can construct queries in any order. Similar issues can come up with projection and sort. For example:
In this case the sort will actually be applied on 'newField' before applying the projection. A user ran into this in
This can also come up with limit and sort. For example:
will perform the limit after the sort, even though the limit was specified first.
The issue is particularly confusing with projection, because in 4.4 we added the ability to use expressions in the projection argument to find(). Many users may try to compute a field using this new feature and expect that they can sort on it (again, see
We should update a few of the documentation pages about find() to discuss this. There is already a note about it here, but we should add a note under the new projection functionality here. It would also be good to specify the full order of operations in the find command page, and perhaps the find() shell helper page.
The wording for this will be a bit tricky because, as mentioned, the query is executed as if the operations were performed in the order listed above. Our optimization code is free re-order the components as long as it doing so does not alter the semantics of the query.