MongoDB $or Aggregation Pipeline Operator

In MongoDB, the $or aggregation pipeline operator evaluates one or more expressions and returns true if any evaluate to true. Otherwise it returns false.

Syntax

The syntax goes like this:

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

Example

Suppose we have a collection called data with the following document:

{ "_id" : 1, "a" : 10, "b" : 2, "c" : 20 }

Here’s what happens when we use $or to test for two conditions against that document:

db.data.aggregate(
   [
     { $match: { _id: 1 } },
     { $project: { 
        _id: 0,
        a: 1,
        b: 1,
        result: { $or: [ 
              { $gt: [ "$a", 9 ] }, 
              { $lt: [ "$b", 1 ] }
            ] } 
          } 
         }
   ]
)

Result:

{ "a" : 10, "b" : 2, "result" : true }

We can see that $or returned true, even though one of the expressions resolves to false. This is because it returns true if any of the expressions are true.

Here it is again, but with both expressions resolving to false.

db.data.aggregate(
   [
     { $match: { _id: 1 } },
     { $project: { 
        _id: 0,
        a: 1,
        b: 1,
        result: { $or: [ 
              { $gt: [ "$a", 20 ] }, 
              { $lt: [ "$b", 1 ] }
            ] } 
          } 
         }
   ]
)

Result:

{ "a" : 10, "b" : 2, "result" : false }

More than Two Arguments

As mentioned, $or accepts one or more expressions. The previous example uses two expressions. Here’s an example that uses three:

db.data.aggregate(
   [
     { $match: { _id: 1 } },
     { $project: { 
        _id: 0,
        a: 1,
        b: 1,
        c: 1,
        result: { $or: [ 
              { $gt: [ "$a", 9 ] }, 
              { $lt: [ "$b", 3 ] }, 
              { $gt: [ "$c", 30 ] } 
            ] } 
          } 
         }
   ]
)

Result:

{ "a" : 10, "b" : 2, "c" : 20, "result" : true }

In this case the result is true, even though the third value evaluates to false.

One Argument

Given that $or accepts one or more expressions, it’s possible to provide a single argument.

Example:

db.data.aggregate(
   [
     { $match: { _id: 1 } },
     { $project: { 
        _id: 0,
        a: 1,
        result: { $or: [ 
              { $gt: [ "$a", 9 ] }
            ] } 
          } 
         }
   ]
)

Result:

{ "a" : 10, "result" : true }

Zero, Null, and Undefined Values

The $or operator evaluates 0, null, and undefined as false.

Suppose we have the following documents:

{ "_id" : 2, "a" : 0, "b" : 2 }
{ "_id" : 3, "a" : 10, "b" : 0 }
{ "_id" : 4, "a" : 0, "b" : 0 }
{ "_id" : 5, "a" : null, "b" : 2 }
{ "_id" : 6, "a" : 10, "b" : null }
{ "_id" : 7, "a" : null, "b" : null }
{ "_id" : 8, "a" : undefined, "b" : 2 }
{ "_id" : 9, "a" : 10, "b" : undefined }
{ "_id" : 10, "a" : undefined, "b" : undefined }

Here’s what happens when we apply $or:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ] } } },
     { $project: { 
        _id: 0,
        a: 1,
        b: 1,
        result: { $or: [ "$a", "$b" ] } }
         }
   ]
)

Result:

{ "a" : 0, "b" : 2, "result" : true }
{ "a" : 10, "b" : 0, "result" : true }
{ "a" : 0, "b" : 0, "result" : false }
{ "a" : null, "b" : 2, "result" : true }
{ "a" : 10, "b" : null, "result" : true }
{ "a" : null, "b" : null, "result" : false }
{ "a" : undefined, "b" : 2, "result" : true }
{ "a" : 10, "b" : undefined, "result" : true }
{ "a" : undefined, "b" : undefined, "result" : false }

Here, I simply used the field as the expression.

Here’s what it looks like when we apply just one argument to the fields that contain 0, null, and undefined:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 4, 7, 10 ] } } },
     { $project: { 
        _id: 0,
        a: 1,
        result: { $or: [ "$a" ] } }
         }
   ]
)

Result:

{ "a" : 0, "result" : false }
{ "a" : null, "result" : false }
{ "a" : undefined, "result" : false }

All fields return false.

Invoke $or with No Arguments

When invoked with no arguments, the $or operator evaluates to false.

Example:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 1 ] } } },
     { $project: { 
        _id: 0,
        result: { $or: [ ] } }
         }
   ]
)

Result:

{ "result" : false }