How to Query With an OR Condition in MongoDB
-
the
$orOperator in MongoDB -
Clauses and Indexes for the
$orOperator -
Single Queries and the
$orOperator -
the
$orOperator VS the$inOperator -
Error Handling With the
$orOperator
This tutorial article will teach you how to use the $or operator with a MongoDB query.
MongoDB provides the user with different types of logical query operators, and the $or operator is one of them. This operator performs logical OR operations on the array of two or more expressions and selects or retrieves only those documents that match at least one of the given expressions in the array.
the $or Operator in MongoDB
You can use the $or operator in methods like find(), update(), etc.
- You can also use this operator with text queries,
GeoSpatialqueries, and sort operations. - When MongoDB evaluates the clauses in the
$orexpression, it performs a collection scan. If indexes support all the clauses in the$orexpression, then MongoDB performs index scans. - You can also nest
$oroperation.
The $or operator chooses documents that meet at least one of the expressions by performing a logical OR operation on an array that consists of two or more expressions.
Syntax:
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
Query:
db.inventory.find( { $or: [ { quantity: { $lt: 50 } }, { price: 20 } ] } )
All documents will be returned in this query in the inventory collection with a quantity field value of less than 50 or a price field value of less than 20.
Clauses and Indexes for the $or Operator
MongoDB either does a collection scan or index scan when evaluating the clauses in the $or expression. If indexes support all of the clauses, MongoDB performs index scans.
Therefore, all clauses in the $or expression must be supported by indexes for MongoDB to use to evaluate the statement. MongoDB will execute a collection scan if this is not the case.
Each clause of a $or query can utilize its index when employing indexes with $or queries.
Query:
db.inventory.find( { $or: [ { quantity: { $lt: 50 } }, { price: 20 } ] } )
Instead of a compound index, you’d establish one index for quantity and another for the price to handle this query.
Query:
db.inventory.createIndex( { quantity: 1 } )
db.inventory.createIndex( { price: 1 } )
To enable $or clauses, MongoDB can use any indexes except the geoHaystack index.
Single Queries and the $or Operator
-
The
$textqueries and the$oroperator.An index must support all clauses in the
$orarray if$orincludes a$textquery. A$textquery must utilize an index, and$orcan only use indexes if all of its clauses are supported by the indexes.If the
$textquery cannot use an index, the query will produce an error. -
GeoSpatialqueries and the$oroperator.
Except for the near clause (which includes `$nearSphere` and `$near`), `$or` allows geographic clauses. There can't be a near clause with any other clause in `$or.`
-
Sort operations and the
$oroperator.MongoDB may now use indexes that support the
$orclauses when running ‘$or’ queries with a ‘sort().’ The indices were not used in previous versions of MongoDB.
the $or Operator VS the $in Operator
Instead of using the $or operator with expressions that are equality checks for the value of the same field, use the $in operator. Use the $in operator to pick all documents in the 'inventory' collection where the quantity field value is 40 or 1000.
Query:
db.inventory.find ( { quantity: { $in: [40, 1000] } } )
Also, you can nest the $or operator.
Error Handling With the $or Operator
The $or operator handles mistakes in the following way to help the query engine to improve queries.
- If any expression is supplied to the
$oroperator, it causes an error when evaluated separately. The$oroperator containing the expression may cause an error, but this is not guaranteed. - Even if the first expression provided to
$orevaluates totrue,an expression submitted after it may trigger an error.
If $x is 0, for example, the following query will always return an error.
Query:
db.example.find( {
$expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] }
} )
Suppose there is any document where $x has the value 0, which involves multiple expressions passed to $or. The following example might produce an error.
Query:
db.example.find( {
$or: [
{ x: { $eq: 0 } },
{ $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
]
} )
This MongoDB tutorial teaches you how to use the $or operator with a MongoDB query to perform logical OR operations on the array, which consists of two or more expressions, and also select or retrieve only the documents that match at least one of the expressions given in the array.