Skip to content

MongoDB or operator AQSA Spark By {Examples}

  • by

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 as its value. Each condition is specified as an individual document and represents a set of criteria to be met for a document to be considered a match. The collection used for $or operation implementation is as follows.

# Create Collection
db.student.insertMany([
{
_id: 1,
name: “Mickel”,
age: 24,
email: “mickel@gmail.com”,
course: [“Python”, “MongoDB”],
personal: {“cell”: 10918129, “city”:”Austin”}

},
{
_id: 2,
name: “Elena”,
age: 20,
email: “elena@gmail.com”,
course: [“Java”, “MongoDB”],
personal: {“cell”: 8190101, “city”:”Houston”}
},
{
_id: 3,
name: “Caroline”,
age: 25,
email: “caroline@gmail.com”,
course: [“C++”, “SQL”],
personal: {“cell”: 3627791, “city”:”New York”}
},
{
_id: 4,
name: “Jimmy”,
age: 23,
email: “jimmy@gmail.com”,
course: [“Java”, “PHP”],
personal: {“cell”: 1337691, “city”:”Austin”}
}
]
)

1. Using $or operator in MongoDB

As mentioned earlier, $or operator performs logical OR operation between two or more conditions. Let’s implement the query of the $or operator like below.

# Usage of OR operator in MongoDB
db.student.find({$or: [{name : “Mickel”}, {email : “elena@gmail.com”}]})

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 as the output returns two documents below.

2. Using $or operator with the document field 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.

Thus, we retrieved the document in the output, which matched both conditions.

3. Using $or operator with the conditional operator to get the value in a range in MongoDB

However, 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 conditional operator
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.

4. Using $or operator for the array in MongoDB

The $or operator also be deployed over the array of documents for performing the logical OR operator. The query of $or operator is provided for the array field.

# 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 for an embedded field in MongoDB

Similarly, the $or operator can also be used for OR logical operation over the embedded document of the collection. There, 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”}]})

The MongoDB query is called the $or operator, which is used to take the two conditions. 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.

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.

More details about this topic can be found here.

 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 }, {  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *