Interesting: $in can be replaced with (value, value)?

db.movieDetails.find({rated:(“PG”,“PG-13”), “runtime”:{$gte:90} }, { _id:0, title:1, rated:1})

No, if you try running two different queries, you can see the difference in the output. Here is one query using $in:

db.movieDetails.find({ rated:{ $in: ["PG","PG-13"] } })

This will be different from this query:

db.movieDetails.find({ rated: ("PG","PG-13") })

I would recommend try running these 2 queries and let me know if you see the same output.

Kanika

1 Like

Hmm… interesting.
Looks like it is picking up the last value from those inside the curvy brackets.
See below:

>
> // Query 1
> db.movieDetails.find({"rated": "PG"}).count()
108
>
> // Query 2
> db.movieDetails.find({"rated": "PG-13"}).count()
153
>
> // Query 3 : 261 = 108 + 153
> db.movieDetails.find({"rated": {$in: ["PG", "PG-13"]}}).count()
261
>
> // Query 4 - same as Query 2
> db.movieDetails.find({"rated": ("PG", "PG-13")}).count()
153
>
>

Apparently, curvy brackets are legal in Javascript/JSON.
This website mentions that it is an undocumented feature in JSON, and that it defines a “list”.
https://blog.jayway.com/2013/04/01/three-undocumented-features-of-json-3/

Maybe a Javascript or JSON expert can chip in and explain(?)

Upon further testing in the Mongo shell, it looks like (x1, x2, x3, … xn) always returns xn - the last value. And maybe that’s why your query returns the data for rated = “PG-13”.

Here’s another test to confirm that it is picking up the last value:

>
> // Query 5 - same as Query 1
> db.movieDetails.find({"rated": ("PG-13", "PG")}).count()
108
>
>

More testing - always the last value:

>
> (1, 2, 3)
3
>
> (1)
1
>
> (1, 2, 3, 4, 5, 6, 7, 8, 9)
9
>
> x = (1, 2, 3, 4, 5)
5
>
> x
5
>
> x = (1)
1
>
> x
1
>
> x = ()
2019-01-28T13:44:25.473-0500 E QUERY    [thread1] SyntaxError: expected expression, got ')' @(shell):1:5

>
>
1 Like

Here are docs from MDN on the Comma Operator:

I feel like it used to be common practice to do things like:
var a, b, c;

If you want to declare several variables but not initialize them yet, but I feel like that’s frowned upon now for whatever reason.

Anyways, that syntax appears to just be mongo shell being a javascript interpreter. It’s just normal javascript evaluation.

1 Like

Awesome!
Thank you for that nugget of information and for linking the documentation!