MongoDB multiple OR conditions on same key?

To apply multiple OR conditions on the same field in MongoDB, use the $or operator with an array of condition objects. Each condition can target the same field with different values or operators.

Syntax

db.collection.find({
    $or: [
        { "fieldName": "value1" },
        { "fieldName": "value2" },
        { "fieldName": { $operator: "value3" } }
    ]
});

Sample Data

db.demo551.insertMany([
    { "Name": "John" },
    { "Name": "Chris Brown" },
    { "Name": "John Doe" },
    { "Name": "John Smith" },
    { "Name": "Carol" }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e8e36d39e5f92834d7f05e5"),
        ObjectId("5e8e36d89e5f92834d7f05e6"),
        ObjectId("5e8e36de9e5f92834d7f05e7"),
        ObjectId("5e8e36e59e5f92834d7f05e8"),
        ObjectId("5e8e36ec9e5f92834d7f05e9")
    ]
}

Example: Multiple OR Conditions on Same Key

Find documents where Name equals "Carol" OR "John" ?

db.demo551.find({
    $or: [
        { "Name": { $eq: "Carol" } },
        { "Name": { $eq: "John" } }
    ]
});
{ "_id": ObjectId("5e8e36d39e5f92834d7f05e5"), "Name": "John" }
{ "_id": ObjectId("5e8e36ec9e5f92834d7f05e9"), "Name": "Carol" }

Alternative: Using $in Operator

For simple equality checks on the same field, $in is more concise ?

db.demo551.find({
    "Name": { $in: ["Carol", "John"] }
});

Key Points

  • Use $or for multiple conditions on the same field with different operators.
  • Use $in when checking equality against multiple values on the same field.
  • Each condition in the $or array is evaluated independently.

Conclusion

The $or operator allows multiple conditions on the same MongoDB field. For simple equality checks, consider using $in as a more efficient alternative.

Updated on: 2026-03-15T03:32:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements