[SERVER-46788] Suggestion: alias $any to $in Created: 11/Mar/20  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Sina Siadat Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-44355 Add $some array query operator Backlog
related to DOCS-13916 Describe $in in the article for query... Closed
Assigned Teams:
Query Optimization
Sprint: Query 2020-09-21, Query 2020-10-05
Participants:

 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.

 



 Comments   
Comment by Katya Kamenieva [ 18/Aug/20 ]

Hi siadat@gmail.com, thank you for bringing this up. Although this update is not currently scheduled, we will consider your suggestion when working on the usability improvements of the query language to make sure everything is consistent.

Comment by Carl Champain (Inactive) [ 11/Mar/20 ]

Hi siadat@gmail.com,

Thanks for taking the time to submit this report!
We are passing this ticket along to the appropriate team for further investigation. Updates will be posted on this ticket as they happen.

Kind regards,
Carl

Comment by Sina Siadat [ 11/Mar/20 ]

Furthermore, the proposed operator name is consistent with both scalar as well as array fields.

db.inventory.find({ tags: { $any: ["red", "blue" ] } }) // tags array includes ANY item IN the given array
db.inventory.find({ title: { $any: [ "Mr", "Mrs" ] } }) // title string is ANY of the items IN the given array

And for the sake of completeness:

db.inventory.find({ tags: { $all: [ "red", "blue" ] } }) // tags array includes ALL items IN the given array

 Note that all of three lines is read using "in". What distinguishes them are the words "any" and "all". 

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