Isaac Borrero
Isaac Borrero

Reputation: 33

What is happening in this MongoDB Query?

{ $or:[{TextField1:{$eq:"DX93E6XX6R"}}], $or:[{TextField1:{$eq:"N4JXFTNP64"}}] }

There are two documents in the collection. One with a "TextField1" value equal to the first expression and one with the same field equal to the second expression.

My thought was:

Find all documents where TextField1 equals "DDX93E6XX6R" AND TextField1 equals "N4JXFTNP64"

Upvotes: 0

Views: 24

Answers (2)

JohnnyHK
JohnnyHK

Reputation: 311895

It's not a sensible query because you can't have two fields named $or in the same object so only the second one will be used (overwriting the first one). And an $or with one element is the same as just that element.

So it's the same as:

{TextField1:{$eq:"N4JXFTNP64"}}

Which is the same as:

{TextField1: "N4JXFTNP64"}

Which is:

Find all documents where TextField1 equals "N4JXFTNP64"

Upvotes: 2

S. Rhea
S. Rhea

Reputation: 31

If you're looking to match documents with either value, then your statement could be either:

{$or: [{Textfield1: "foo1"}, {Textfield1: "foo2"}]}

or...

{Textfield1: {$in: ["foo1", "foo2"]}}

Upvotes: 0

Related Questions