kamatchi
kamatchi

Reputation: 86

MongoDB: $or operator error

I'm trying to use $or operator but it's returns a error.

My code:

{db.hw1_1.find( { "name": { $in: ["kamatchi","kamala"] } } )

It's returning answer. But this query returning error.

{db.hw1_1.find( { "name": { $or: ["kamatchi","kamala"] } } )

Returns:

error: {
 $err: "can't canonicalize query: BadValue unknown operator: $or   ",
"code" : 1787
}

Upvotes: 2

Views: 5329

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151122

The $in operator basically is an or condition but just in a specialized way. So you can use it as a short form to test a list of values either as some type of object or a regular expression.

On the other hand $or is more terse, so this would be the same as the $in query

db.hw1_1.find({ $or: [{ "name": "kamatchi" }, { "name": "kamala"} ] } )

So $in is best to test multiple values against a single key. Think of $or as a "list of queries" that are treated separately and only one of those queries needs to be satisfied.

Upvotes: 10

Related Questions