In this tutorial, I will discuss how we can use the $or operator in MongoDB.
There are a variety of operators in MongoDB that can be used for many different purposes. Developers can do a lot more than just CRUD in MongoDB-backed applications because it provides easy-to-understand, highly advanced, as well as flexible operators. Perhaps this is one of the driving forces behind MongoDB’s popularity.
In MongoDB, you can customize querying, printing, updating, and do a lot more with the help of operators. Users ranging from beginners to seasoned developers alike enjoy working with this data storage platform. Well, I have decided today to discuss the $or operator in MongoDB since it is one of many operators offered by this database management system.
The $or operator is a logical operator that is used on one or more arrays to check if the results match at least one of the given expressions in the array. The $or operator can be used with methods like find() and update() in MongoDB.
In this article, I will explain to you how you can use the $or operator in MongoDB and demonstrate a few examples so you will be able to grasp the topic better.
$or Operator Syntax
Let us see the syntax of the $or operator so we can set the tone for ourselves of what our commands could look like:
{ $or: [ { Expression1 }, { Expression2 }, ..., { ExpressionN } ] }
Finding Documents Using the $or Operator in MongoDB
Let us look at how we can custom find documents in a MongoDB collection using the $or operator. Let us get started with below mentioned steps to finding documents in the mongo shell.
- Start up your MongoDB shell server
- Pick a database you want to use and move into it.
show dbs
use droneStore
- Now, we have here our drones collection in this database. I have already added a few documents inside beforehand for this guide. Let us first list all of them using find().
> db.drones.find({})
{ "_id" : ObjectId("603502ba24df1c2350e676e9"), "utility" : [ "Delivery" ], "onSale" : false, "name" : "RX - Ferezza S120", "price" : 105499, "weight" : "26 kilograms", "__v" : 0 }
{ "_id" : ObjectId("60352ecb6ce2811cc8892a75"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "SV - LaserX AW205", "price" : 4000, "weight" : "2.3 kilograms", "__v" : 0 }
{ "_id" : ObjectId("6157239d341d653bccaf7822"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "DF - Jetfire Nitro RX-V", "price" : 22000, "weight" : "3 kilograms", "__v" : 0 }
{ "_id" : ObjectId("6157240c341d653bccaf7823"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "Pink Sentinel Q95", "price" : 13000, "weight" : "2 kilograms", "__v" : 0 }
{ "_id" : ObjectId("61573885341d653bccaf7825"), "utility" : [ "Security" ], "onSale" : false, "name" : "SV - Xorvia 9908Y", "price" : 7760, "weight" : "3.8 kilograms", "__v" : 0 }
{ "_id" : ObjectId("615738d6341d653bccaf7826"), "utility" : [ "Photography" ], "onSale" : false, "name" : "SV - Ryee SW657", "price" : 8900, "weight" : "760 grams", "__v" : 0 }
{ "_id" : ObjectId("61573946341d653bccaf7827"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "RV - Zereca Eagle-i", "price" : 3200, "weight" : "1.7 kilograms", "__v" : 0 }
{ "_id" : ObjectId("615739bb341d653bccaf7828"), "utility" : [ "Delivery" ], "onSale" : false, "name" : "OS - Falcon GO!", "price" : 45000, "weight" : "17 kilograms", "__v" : 0 }
{ "_id" : ObjectId("61589f092d0a441e040c3fdc"), "utility" : [ "Photography" ], "onSale" : false, "name" : "DX - Firebird Scarlet Red", "price" : 45000, "weight" : "4.7 kilograms", "__v" : 0 }
{ "_id" : ObjectId("61589f6e2d0a441e040c3fdd"), "utility" : [ "Photography" ], "onSale" : false, "name" : "IJ - Optico X53", "price" : 3900, "weight" : "3 kilograms", "__v" : 0 }
- Now, lets say we want to customize our find() method by adding some logic to it. This is where the $or operator comes in handy. I want to pull out documents that have “utility” as “Security” which is an array, and “weight” as “3 kilograms”. Let’s see how we can do this:
> db.drones.find( { $or: [ { utility: [ `Security` ] }, { weight: `3 kilograms`} ] } ).pretty()
{
"_id" : ObjectId("6157239d341d653bccaf7822"),
"utility" : [
"Recreation"
],
"onSale" : false,
"name" : "DF - Jetfire Nitro RX-V",
"price" : 22000,
"weight" : "3 kilograms",
"__v" : 0
}
{
"_id" : ObjectId("61573885341d653bccaf7825"),
"utility" : [
"Security"
],
"onSale" : false,
"name" : "SV - Xorvia 9908Y",
"price" : 7760,
"weight" : "3.8 kilograms",
"__v" : 0
}
{
"_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
"utility" : [
"Photography"
],
"onSale" : false,
"name" : "IJ - Optico X53",
"price" : 3900,
"weight" : "3 kilograms",
"__v" : 0
}
Tada! The results are so precise! Hence, we have successfully used the $or operator in MongoDB to find documents. If you want you can even nest the $or operator in your query.
Updating Documents Using $or Operator in MongoDB
Now, let us try and update documents in our database using the $or operator in MongoDB. Simply go by the steps mentioned below to get started:
> db.drones.updateMany( { $or: [ { utility: [ `Security` ] }, { weight: `3 kilograms`} ] }, { $set: { price: 10000 } } )
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
- Let’s see what the “price” on our documents now look like after using the $or operator on them to update them:
> db.drones.find( { $or: [ { utility: [ `Security` ] }, { weight: `3 kilograms`} ] } ).pretty()
{
"_id" : ObjectId("6157239d341d653bccaf7822"),
"utility" : [
"Recreation"
],
"onSale" : false,
"name" : "DF - Jetfire Nitro RX-V",
"price" : 10000,
"weight" : "3 kilograms",
"__v" : 0
}
{
"_id" : ObjectId("61573885341d653bccaf7825"),
"utility" : [
"Security"
],
"onSale" : false,
"name" : "SV - Xorvia 9908Y",
"price" : 10000,
"weight" : "3.8 kilograms",
"__v" : 0
}
{
"_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
"utility" : [
"Photography"
],
"onSale" : false,
"name" : "IJ - Optico X53",
"price" : 10000,
"weight" : "3 kilograms",
"__v" : 0
}
Great! With this, we have successfully learned to update documents too using the $or operator in MongoDB.
Read More: Basic 2021 Guide to MongoDB findById Function
Conclusion
Learn to use the $or operator to find and update documents in MongoDB.