Query with OR condition in MongoDB
This article will explain how to use the $or operator in MongoDB queries.
MongoDB provides users with different types of logical query operators, and the $or operator is one of them. This operator performs a logical OR operation on an array of two or more expressions and selects or retrieves only the documents that match at least one of the given expressions in the array.
$or operator in MongoDB
We can use the $or operator in methods like find()
, or.update()
- You can also use this operator for text queries, geospatial queries, and sort operations.
- When MongoDB evaluates the clauses in the $or expression, it performs a collection scan. If an index supports all the clauses in the $or expression, MongoDB performs an index scan.
- It is also possible to nest $or operations.
$or
The operator selects documents that satisfy at least one expression by performing a logical OR operation on an array of two or more expressions.
grammar:
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
Query:
db.inventory.find( { $or: [ { quantity: { $lt: 50 } }, { price: 20 } ] } )
This query will return all documents in the inventory collection where the quantity field value is less than 50 or the price field value is less than 20.
Clauses and Indexes for the $or Operator
When evaluating the clauses in an $or expression, MongoDB performs either a collection scan or an index scan. If the index supports all clauses, MongoDB performs an index scan.
Therefore, all clauses in an $or expression must be backed by an index for MongoDB to use to evaluate the statement. If this is not the case, MongoDB will perform a collection scan.
When you use an index in an $or query, each clause of the $or query can use its index.
Query:
db.inventory.find( { $or: [ { quantity: { $lt: 50 } }, { price: 20 } ] } )
Instead of a compound index, you could have one index for quantity and another for price to handle this query.
Query:
db.inventory.createIndex( { quantity: 1 } )
db.inventory.createIndex( { price: 1 } )
To enable the $or clause, MongoDB can use any index except the geoHaystack index.
Single query and the $or operator
-
$text query and $or operator.
If $or contains a $text query, the index must support all clauses in the $or array. $text queries must use an index, and $or can only use an index if the index supports all of its clauses.
If a $text query cannot use an index, the query will generate an error.
-
GeoSpatial queries and the $or operator.
In addition to the near clause (including $nearSphere and $near), $or allows geographic clauses. $or cannot have a near clause with any other clause.
-
Sorting operations and the $or operator.
MongoDB now uses indexes that support $or clauses when running "$or" queries with "sort()". These indexes were not used in previous versions of MongoDB.
$or Operator vs. $in Operator
Instead of using the $or operator with expressions that check for equality on values of the same field, use the $in operator. Use the $in operator to select all documents in the 'inventory' collection that have a quantity field value of either 40 or 1000.
Query:
db.inventory.find ( { quantity: { $in: [40, 1000] } } )
Additionally, you can nest $or operators.
Using the $or operator to handle errors
The $or operator handles errors in the following ways to help the query engine improve the query.
- If any expression is supplied to the $or operator, it will cause an error when evaluated alone. The $or operator containing an expression may cause an error, but it is not guaranteed.
- Even if the first expression supplied to $or evaluates to true, expressions submitted after it may trigger an error.
For example, if $x is 0, the following query will always return an error.
Query:
db.example.find( {
$expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] }
} )
Assume there is any document where $x evaluates to 0, which involves multiple expressions passed to $or. The following example may produce an error.
Query:
db.example.find( {
$or: [
{ x: { $eq: 0 } },
{ $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
]
} )
This MongoDB article explains how to use the $or operator in MongoDB queries to perform a logical OR operation on an array of two or more expressions and select or retrieve only the documents in the array that match at least one of the given expressions.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
MongoDB starts with a query
Publish Date:2025/04/10 Views:195 Category:MongoDB
-
In this MongoDB article, users will learn how to start queries using $regex. It provides regular expression functionality for pattern matching strings in queries. MongoDB starts querying using $regex If you want to use $regex , use one of t
MongoDB 从查询开始
Publish Date:2023/05/11 Views:188 Category:MongoDB
-
在这篇 MongoDB 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。
MongoDB 中带有 OR 条件的查询
Publish Date:2023/05/11 Views:218 Category:MongoDB
-
$or 运算符用于对由两个或多个表达式组成的数组执行逻辑或运算,并仅选择或检索与数组中给定的至少一个表达式匹配的文档。MongoDB 中的 $or 运算符
MongoDB 中查询数组大小大于1的文档
Publish Date:2023/05/10 Views:470 Category:MongoDB
-
在处理需要验证数组大小或查找大小大于或小于特定长度的元素的项目时,可能会使用 $size、$where、$exists 等 MongoDB 运算符。下面讨论的方法可以帮助您解决数组长度或数组大小问题。
MongoDB 中不区分大小写的查询
Publish Date:2023/05/10 Views:189 Category:MongoDB
-
在本文中,将简要详细地讨论不区分大小写的查询。 此外,还详细解释了不区分大小写的搜索查询。MongoDB 中不区分大小写的查询
在 MongoDB 中使用多个条件进行查询
Publish Date:2023/05/10 Views:1194 Category:MongoDB
-
本文将教您如何使用 MongoDB 中的多个文档进行多条件查询。 $and 和 $or 运算符通过每个示例进行了简要说明。MongoDB 为用户提供了不同的逻辑查询操作符,$or 和$and 操作符是其一。
MongoDB 查询嵌套对象
Publish Date:2023/05/10 Views:221 Category:MongoDB
-
在本篇文章中,我们将学习如何查询 MongoDB 中的嵌套对象。在 MongoDB 中查询嵌套对象 MongoDB 提供读取操作以从集合中检索嵌入或嵌套数据或搜索嵌入或嵌套文档。
在 MongoDB 中查询字符串长度
Publish Date:2023/05/10 Views:161 Category:MongoDB
-
在本文中,我们将了解如何在 MongoDB 中使用字符串长度查找特定文档,并且我们还将看到有关该主题的示例,以便于理解。根据字符串长度在集合中查找特定文档