Home > Back-end >  Mongodb $and operator vs. implicit and
Mongodb $and operator vs. implicit and

Time:10-05

I am learning Mongodb MQL. The documentation for the "$and" operator states that there is an "implicit-and" when two expressions are separated by a comma, that can be used instead.

So, running a test aggregation in Compass, I have...

{ $and:[ {VRT_Cvert: {$gte: -1.41}}, {VRT_Cvert: {$lte: -1.39}} ] }

and also, in a separate stage...

{ VRT_Cvert: {$gte: -1.41}, VRT_Cvert: {$lte: -1.39} }

...such that I can turn each stage on and off, to see the results.

I do NOT get the same result with these two different options. The implicit-and is including one document that does not fall within the specified range. Is there some kind of known bug with the implicit-and technique? Or, do I have some kind of syntax problem with my implicit-and statement?

CodePudding user response:

For implicit-and you cannot use same key twice , you can use only different keys separated by coma ...

from the docs: MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.

CodePudding user response:

To use different multiple criteria with a single field in an implicit $and, combine the criteria into a single object, like:

{ VRT_Cvert: {$gte: -1.41, $lte: -1.39} }
  • Related