Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-46788

Suggestion: alias $any to $in

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • Querying
    • None
    • Query Optimization
    • Query 2020-09-21, Query 2020-10-05

    Description

      When reading queries with $in, I find it more appropriate to mentally replace the $in operator with $any. Compare the following two queries which could cause confusion for the developer and lead to silent application bugs:

      db.inventory.find({ tags: { $in: ["red", "blue" ] } })
      db.inventory.find({ tags: { $all: ["red", "blue" ] } })

      Note that even when reading an $all query, the developer might use the word "in". For example, I read the $all query above as: "documents whose tags array includes all items in the given array".

      However, replacing $in with $any, I would read the query as "documents whose tags array includes any item in the given array". This matches the behavior of the $in operator more accurately than the in word "in" the $in operator itself. In fact, the documentation for the $in operator uses the word "any" along with "in" to phrase the description of $in:

      The $in operator selects the documents where the value of a field equals any value in the specified array.

      Also in the queries Query and Projection Operators documentation:

      Matches any of the values specified in an array.

      While $in does make sense on its own, it is easy to confuse the behavior of $in and $all with the current terminology. An alias like $any would clarify the confusion and obviate many bugs and unnecessary printf debugging for the developers.

       

      Attachments

        Activity

          People

            backlog-query-optimization Backlog - Query Optimization
            siadat@gmail.com Sina Siadat
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

            Dates

              Created:
              Updated: