In this tutorial, I will cover the $set operator in MongoDB.
The $set operator replaces the value of a given field with a given value. If the $set operator is given a field that does not exist, then it will create a new field and assign the given value to it. however, if that field meets a type constraint, the operation is likely to throw an error.
This guide will give you different examples of how to use the $set operator in MongoDB. let us get started with this powerful guide!
Also read: $unset Operator in MongoDB: Quick Guide
The $set Operator Syntax in MongoDB
Below is the syntax for the $set operator in MongoDB when using in the mongo shell:
{ $set: { field1: value1, ... } }
Using the $set Operator in MongoDB
Let us get started with the examples of using the $set operator in MongoDB. We will first take a look at the available documents inside our “cats collection” that we want to work in:
> db.cats.find().pretty()
{
"_id" : ObjectId("602eb6c728ff814b64eb3411"),
"name" : "Stephanie",
"age" : 22,
"breed" : "Persian",
"personality" : {
"dogFriendly" : true,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602eb73728ff814b64eb3412"),
"name" : "Whiskers",
"age" : 4,
"breed" : "Scottish Fold",
"personality" : {
"dogFriendly" : false,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602eb84628ff814b64eb3413"),
"name" : "Krissy",
"age" : 6,
"breed" : "Turkish Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebc5ea78db75e09057cd0"),
"name" : "Cassy",
"age" : 16,
"breed" : "Japanese Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebc9da78db75e09057cd1"),
"name" : "Shane",
"age" : 11,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebed7a78db75e09057cd2"),
"name" : "Phoebe",
"age" : 21,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
We have in total 6 cats documents in our collection.
Updating Values of Top-Level Fields Using the $set Operator in MongoDB
This section will focus on setting or updating or modifying values of a top-level that is a non-nested field in MongoDB.
Let’s say we want to modify the breed’s name for Stephanie from Persian to Persian Cat.
> db.cats.update(
... { "name" : "Stephanie" },
... { $set: { "breed": "Persian Cat"} }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.cats.find().pretty()
{
"_id" : ObjectId("602eb6c728ff814b64eb3411"),
"name" : "Stephanie",
"age" : 22,
"breed" : "Persian Cat",
"personality" : {
"dogFriendly" : true,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602eb73728ff814b64eb3412"),
"name" : "Whiskers",
"age" : 4,
"breed" : "Scottish Fold",
"personality" : {
"dogFriendly" : false,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602eb84628ff814b64eb3413"),
"name" : "Krissy",
"age" : 6,
"breed" : "Turkish Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebc5ea78db75e09057cd0"),
"name" : "Cassy",
"age" : 16,
"breed" : "Japanese Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebc9da78db75e09057cd1"),
"name" : "Shane",
"age" : 11,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
{
"_id" : ObjectId("602ebed7a78db75e09057cd2"),
"name" : "Phoebe",
"age" : 21,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 20000
}
}
Perfect! Staphanie’s breed’s name is now set to Persian Cat.
Updating Values of Nested/Embedded Fields Using the $set Operator
This section will focus on setting or updating or modifying values of an embedded field that is a non-top-level field in MongoDB.
Let’s say we want to sell all our cats for 30000 now instead of 20000.
> db.cats.updateMany(
... { },
... { $set: { "moreDetails.cost": 30000 } }
... )
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.cats.find().pretty()
{
"_id" : ObjectId("602eb6c728ff814b64eb3411"),
"name" : "Stephanie",
"age" : 22,
"breed" : "Persian Cat",
"personality" : {
"dogFriendly" : true,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 30000
}
}
{
"_id" : ObjectId("602eb73728ff814b64eb3412"),
"name" : "Whiskers",
"age" : 4,
"breed" : "Scottish Fold",
"personality" : {
"dogFriendly" : false,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 30000
}
}
{
"_id" : ObjectId("602eb84628ff814b64eb3413"),
"name" : "Krissy",
"age" : 6,
"breed" : "Turkish Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
}
}
{
"_id" : ObjectId("602ebc5ea78db75e09057cd0"),
"name" : "Cassy",
"age" : 16,
"breed" : "Japanese Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
}
}
{
"_id" : ObjectId("602ebc9da78db75e09057cd1"),
"name" : "Shane",
"age" : 11,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
}
}
{
"_id" : ObjectId("602ebed7a78db75e09057cd2"),
"name" : "Phoebe",
"age" : 21,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
}
}
Great! We have now changed the cost of our cats to 30000.
Updating Values of Non-Existent Array Fields Using the $set Operator
This section will focus on setting a non-existent array field to all our documents.
We will be adding a likes array field for all our cats.
> db.cats.updateMany(
... { },
... { $set: { "likes": ["fish", "milk", "chicken", "blankets"] } }
... )
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.cats.find().pretty()
{
"_id" : ObjectId("602eb6c728ff814b64eb3411"),
"name" : "Stephanie",
"age" : 22,
"breed" : "Persian Cat",
"personality" : {
"dogFriendly" : true,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
{
"_id" : ObjectId("602eb73728ff814b64eb3412"),
"name" : "Whiskers",
"age" : 4,
"breed" : "Scottish Fold",
"personality" : {
"dogFriendly" : false,
"childFriendly" : true
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
{
"_id" : ObjectId("602eb84628ff814b64eb3413"),
"name" : "Krissy",
"age" : 6,
"breed" : "Turkish Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
{
"_id" : ObjectId("602ebc5ea78db75e09057cd0"),
"name" : "Cassy",
"age" : 16,
"breed" : "Japanese Angora",
"personality" : {
"dogFriendly" : true,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
{
"_id" : ObjectId("602ebc9da78db75e09057cd1"),
"name" : "Shane",
"age" : 11,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
{
"_id" : ObjectId("602ebed7a78db75e09057cd2"),
"name" : "Phoebe",
"age" : 21,
"breed" : "Bobtail",
"personality" : {
"dogFriendly" : false,
"childFriendly" : false
},
"moreDetails" : {
"cost" : 30000
},
"likes" : [
"fish",
"milk",
"chicken",
"blankets"
]
}
Perfect! The likes array field has now been added to all our documents.
Read More: Node.js Tutorial for Beginners Step by Step With Examples
Conclusion
In this article, we learned to use the $set operator with examples here. To learn more on MongDB, follow CodeForGeek.