New in version 1.6.

Changed in version 2.0: You may nest $or operations; however, these expressions are not as efficiently optimized as top-level.

The $or operator performs a logical OR operation on an array of two or more <expressions> and selects the documents that satisfy at least one of the <expressions>. The $or has the following syntax:

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

Consider the following example:

db.inventory.find( { $or: [ { qty: { $lt: 20 } }, { sale: true } ] } )

This query will select all documents in the inventory collection where either the qty field value is less than 20 or the sale field value is true.


$or Clauses and Indexes

When using indexes with $or queries, each clause of an $or will execute in parallel. These clauses can each use their own index. Consider the following query:

db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ] } )

To support this query, rather than a compound index, you would create one index on price and another index on sale:

db.inventory.ensureIndex( { price: 1 } )
db.inventory.ensureIndex( { sale: 1 } )

$or and Sort Operations

When using the $or operator with the sort() method, the query will not use the indexes on the $or fields. Consider the following query which adds a sort() method to the above query:

db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ] } ).sort( { item:1 } )

This modified query will not use the index on price nor the index on sale.

$or and text Queries

If $or includes a $text query, all clauses in the $or array must be indexed.

$or and GeoSpatial Queries

Changed in version 2.6.

$or supports geospatial clauses with the following exception for the near clause (near clause includes $nearSphere and $near). $or cannot contain a near clause with any other clause.

$or versus $in

When using $or with <expressions> that are equality checks for the value of the same field, use the $in operator instead of the $or operator.

For example, to select all documents in the inventory collection where the qty field value equals either 20 or 50, use the $in operator:

db.inventory.find ( { qty: { $in: [20, 50] } } )

See also

$and, find(), sort(), $in