Update MongoDB Using Set Operator
Introduction
When you’re working with data stored in MongoDB, there are many situations in which you’ll need to update the value of a field in a collection. For example, an employee may have been promoted to a new position, and that information needs to be updated in a MongoDB document. Perhaps a student moved to a new address, and their contact information needs to be changed in MongoDB. Regardless of the exact circumstances, it’s easy to make these changes with the help of the $set
operator. In this article, we’ll explain how to update MongoDB using the set operator, providing multiple examples to illustrate how it’s done.
Prerequisites
Before you attempt to follow along with the instructions in this tutorial, make sure that MongoDB is installed and configured. If you haven’t installed the software yet, you can download it here.
What Is the MongoDB $Set Operator?
The $set
operator allows us to change the value of a selected MongoDB field with a new value or set of values, If the field does not exist, then it will be created at that point, and it will contain the specified value.
We can see the basic form of the $Set
operator below:
1 | {$set:{field_1: value_1, field_2: value_2, ..... }} |
Sample Data Set
Throughout this tutorial, we’ll be working with a collection named employee
. This sample collection will contain the following documents:
1 2 3 4 5 | { "_id" : "1001", "name" : "james", "lastname" : "trump", "department" : "ict", "age" : 20 } { "_id" : "1002", "name" : "rupert", "lastname" : "ledesma", "department" : "finance", "age" : 40 } { "_id" : "1003", "name" : "mark", "lastname" : "jones", "department" : "ict", "age" : 21 } { "_id" : "1004", "name" : "john", "lastname" : "esposo", "department" : "marketing", "age" : 22 } { "_id" : "1005", "name" : "james", "lastname" : "trump", "department" : "ict", "age" : 20 } |
If you’re planning to follow along with this tutorial, it will be helpful to create a sample collection containing these same documents in your MongoDB environment.
Update a Field Using $Set Operator Example
In this example, we’ll update a field value in the employee
collection. We will change the value of name
and age
:
1 2 3 4 5 6 7 8 | db.employee.update( {_id:"1001"}, {$set: {name:"gregory", age: 19 } } ) |
After we execute the command, we should see a response like this:
1 | WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) |
This tells us that one document matched our criteria and was modified.
We can also verify that our update was successful using the following command: db.employee.find({_id:"1001"})
The result should look like this:
1 | { "_id" : "1001", "name" : "gregory", "lastname" : "trump", "department" : "ict", "age" : 19 }` |
We can see that the name was indeed changed to the new specified value “gregory”. Our update operation using the $set
operator was successful.
Update a Sub-Document Using $Set Operator Example
The next example will update a MongoDB sub-document. Before we look at the command, let’s review the following MongoDB collection:
1 2 | { "_id" : "1001", "name" : "Lenie", "lastname" : "Estefan", "department" : "ict", "age" : 20, "contact" : [ { "email" : "lenie@example.com", "mobile" : "221-2347" } ] } { "_id" : "1002", "name" : "Joshua", "lastname" : "Spencer", "department" : "finance", "age" : 20, "contact" : [ { "email" : "joshua@example.com", "mobile" : "231-2907" } ] } |
We’ll use the command shown below to change a MongoDB sub-document, modifying the value of the email
field:
1 2 3 4 5 6 7 8 | db.personnel.update( {_id: "1002"}, {$set: { contact: {email: "joshua20@example.com"} } } ) |
Let’s verify the result of our update operation using the .find()
command, We should get a result that looks something like this:
1 2 | > db.personnel.find({_id: "1002"}) { "_id" : "1002", "name" : "Joshua", "lastname" : "Spencer", "department" : "finance", "age" : 20, "contact" : { "email" : "joshua20@example.com" } } |
The result confirms that the value of email
was modified successfully.
Conclusion
If you use MongoDB to store and manage your data, it’s important to know how to update existing documents in your collections. In this article, we showed you how to update MongoDB using the set operator. With our examples to get you started, you’ll be prepared to update documents in your own MongoDB collections.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started