[DOCS-13808] [Server] Make documentation about order of operations in find() more clear Created: 04/Aug/20  Updated: 22/Jan/24

Status: Backlog
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Ian Boros Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: backlog, feature, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-50080 sort not working on projection create... Closed
Participants:
Days since reply: 3 years, 27 weeks, 1 day ago
Epic Link: DOCSP-11702

 Description   

Description

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:

db.c.find({a:{$gt: 3}}, {newField: {$add: ["a$", "$b"]}}).sort({newField: 1}) 

In this case the sort will actually be applied on 'newField' before applying the projection. A user ran into this in SERVER-50080.

This can also come up with limit and sort. For example:

db.c.find({a:1}).limit(10).sort({a:1}) 

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 SERVER-50080).

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.

Scope of changes

Impact to Other Docs

MVP (Work and Date)

Resources (Scope or Design Docs, Invision, etc.)


Generated at Thu Feb 08 08:08:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.