• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:10 mins read

In MongoDB, the $or operator is a type of logical operator that performs a logical OR operation on two or more expressions and returns the documents that match any of the expressions. Let’s begin with the syntax of $or operator in MongoDB and using $or in queries.


# Syntax
db.collection.find({ $or: [{ cond1 }, { cond2 }, ...{ condN } ]})

The $or operator takes more than one condition/expression as its value. The $or operator can be used in a query to match documents where at least one of the given expressions is true.

In order to explain the $or operator with an example, lets create a collection with few documents.

# Create Collection
         _id: 1,
         name: "Mickel",
         age: 24,
         email: "[email protected]",
         course: ["Python", "MongoDB"],
         personal: {"cell": 10918129, "city":"Austin"}
         _id: 2,
         name: "Elena",
         age: 20,
         email: "[email protected]",
          course: ["Java", "MongoDB"],
         personal: {"cell": 8190101, "city":"Houston"}
         _id: 3,
         name: "Caroline",
         age: 25,
         email: "[email protected]",
         course: ["C++", "SQL"],
         personal: {"cell": 3627791, "city":"New York"}
         _id: 4,
         name: "Jimmy",
         age: 23,
         email: "[email protected]",
         course: ["Java", "PHP"],
         personal: {"cell": 1337691, "city":"Austin"}

1. Using $or operator in MongoDB

As mentioned earlier, the MongoDB $or operator performs logical OR operation between two or more conditions. You can use the $or operator with any number of expressions, each enclosed in its own set of curly braces {}. All the expressions must evaluate to true for the document to be considered a match.

Let’s implement the query using the $or operator.

# Usage of OR operator in MongoDB
db.student.find({$or: [{name : "Mickel"}, {email : "[email protected]"}]})

Here, the MongoDB query is defined with the $or operator that includes two conditions. The first one specifies that the name field should be equal to Mickel, and the second one specifies that the email field should be equal to the given email. Here, the $or operator specifies that either one or both of the conditions must be satisfied for a document to be included in the search results.

Hence, both conditions of the $or operator are matched, so the output returns two documents below.

OR operator in MongoDB

2. Combining $or operator with another condition in MongoDB

Although, the $or operator can be used with another field in MongoDB. For this, we have provided the query which will perform $or operator with another field below.

# Usage of $or operator with document field
db.student.find( { "name" : "Elena" , $or : [{"age" : 23},{"age" : 24},{"age":20}] } )

The MongoDB query is specified with two conditions: the first condition is that the name field should be equal to Elena, and the second condition is that the age field should be equal to either 23, 24, or 20. For the second condition, we deployed the $or operator, which indicates the logical OR operation between the three age conditions.

The above example yields the below output.

3. Using $or operator with the range in MongoDB

Alternatively, the $or operator can be passed with the conditional operator to retrieve the document where the given values are in range.

# Usage of $or operator with range
db.student.find({$or: [{ age: {$lt: 23} },{ age: {$gt: 24} }]})

The MongoDB query is invoked with the $or operator for the logical OR operation between two conditions, each of which specifies a range of values for the age field.  Specifically, the first condition specifies that the age field should be less than 23 as the $lt conditional operator is invoked, while the second condition uses the $gt operator to specify that it should be greater than 24.

The output is retrieved with the two documents which fulfill the criteria of the $or operator.

OR operator in MongoDB

4. Using $or operator with the array in MongoDB

The $or operator also be used with the field on type array. Here is an example.

# Usage of $or operator for array
db.student.find({$or: [{course: {$in: ["C++", "MongoDB"]}}]})

The MongoDB query here finds documents in the collection where the course array field is either C++ or MongoDB. For this, we have invoked the $or operator, which specifies that the course field should be one of the values in the given array. The $or operator passed with the $in operator to match one of the values in the array.

Three documents satisfy the condition of $or operator for the array field.

5. Using $or operator with an embedded field in MongoDB

Similarly, the $or operator can also be used over the embedded document of the collection. Here, we have given a query for implementing the $or operator for matching values in embedded documents.

# Usage of $or operator for an embedded field
db.student.find({$or: [{"personal.cell": 10918129},{"personal.city": "Austin"}]})

Here, the first condition indicates that the cell field within the personal nested document should be equal to the given number, while the second condition specifies that the city field within the personal nested document should be equal to Austin.

The expected documents in the output are yielded from the above query of $or operator.

OR operator in MongoDB

6. Conclusion

In conclusion, we came to know that multiple alternative conditions can be defined using the $or operator, and MongoDB will fetch documents that satisfy any of those conditions. We have provided some examples where the $or operator is used to get the document by combining the conditions.

Note that the $or operator can have a performance impact on your queries, especially if you’re using it with multiple expressions. In general, it’s a good idea to limit the number of expressions you use with the $or operator and to use other query optimization techniques like indexes to improve performance.

More details about this topic can be found here.