Suggestion: alias $any to $in

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Query Optimization
    • Query 2020-09-21, Query 2020-10-05
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

       

            Assignee:
            [DO NOT USE] Backlog - Query Optimization
            Reporter:
            Sina Siadat
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: