mongoDB – $or operator

$or Operator in mongoDB

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 expression.

Syntax:

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

Suppose we have an inventory collection. we want to select document where quantiry is less than 20 or price is 10.

We will use $or and after colon (:) we pass an array with columns we need to perform any operation.

 

> db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } );

This query will find all document that have quantity less than 20 or price is 10.

If we have a students database with  marks collection and we want to select those students whose score is less that 50 or greater than 90 then we will use following query.

 

db.scores.find({$or:[{score:{$lt:50}},{score:{$gt:90} }]});

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.