Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-13808

[Server] Make documentation about order of operations in find() more clear

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: manual, Server
    • Labels:
      None

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              ian.boros Ian Boros
              Participants:
              Last commenter:
              Jeffrey Allen
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Days since reply:
                10 weeks, 6 days ago