Introduction to MongoDB $or Operator

The $or operator is an operator in MongoDB used to perform logical OR operations on multiple conditions. It can combine multiple query conditions to form a logical OR relationship and query documents that meet any of the conditions.

Syntax

The syntax of the $or operator is as follows:

{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

Where <expression> is a query condition and can be any valid query expression.

Use Cases

The $or operator is mainly used to query documents that need to meet any of multiple query conditions. For example, we may need to query information about all employees who are over 30 years old or have an income greater than 5000. In this case, we can use the $or operator to combine these two query conditions.

Examples

Example 1: Query information about employees who are over 30 years old or have an income greater than 5000

Assume we have a collection employees that contains information about many employees, including their names, ages, incomes, and so on. Now we need to query information about all employees who are over 30 years old or have an income greater than 5000. We can use the $or operator to perform the query:

db.employees.find({ $or: [{ age: { $gt: 30 } }, { income: { $gt: 5000 } }] })

The query results are as follows:

{ "_id" : ObjectId("602db3a3df587c2737c9a08d"), "name" : "Alice", "age" : 32, "income" : 6000 }
{ "_id" : ObjectId("602db3a3df587c2737c9a08e"), "name" : "Lucy", "age" : 28, "income" : 5500 }
{ "_id" : ObjectId("602db3a3df587c2737c9a08f"), "name" : "James", "age" : 29, "income" : 4000 }
{ "_id" : ObjectId("602db3a3df587c2737c9a090"), "name" : "Kobe", "age" : 35, "income" : 4500 }

Example 2: Query information about orders in Beijing or Shanghai

Assume we have a collection orders that contains information about many orders, including order numbers, order dates, order statuses, order amounts, shipping addresses, and so on. Now we need to query information about all orders in Beijing or Shanghai. We can use the $or operator to perform the query:

db.orders.find({
  $or: [
    { "shipping_address.city": "Beijing" },
    { "shipping_address.city": "Shanghai" }
  ]
})

The above query statement will return all order information whose shipping address is in Beijing or Shanghai.

Assuming that an order document in the orders collection is as follows:

{
  "_id": ObjectId("601234567890abcdef123456"),
  "order_number": "202201010001",
  "order_date": ISODate("2022-01-01T00:00:00Z"),
  "status": "Complemted",
  "total_amount": 1500,
  "shipping_address": {
    "province": "Beijing",
    "city": "Beijing"
  }
}

Then executing the above query statement will return this order document.

Conclusion

The $or operator in MongoDB is used to perform a logical OR operation on multiple conditions. It can combine multiple query conditions to form a logical OR relationship, querying documents that meet any of the conditions.

In practical application scenarios, the $or operator is very common and can help us easily implement multi-condition queries.