Mongoose Update
Introduction
The update() method in mongoose is used to update the values of a document in a MongoDB collection. This method only updates the very first matching document in the collection. In this article, we will discuss how to use the mongoose update() method.
We will write our code in the following route handler.
1 | router.route("/update").put(function(req, res) {}); |
We will use the update() method on the details collection.
1 2 3 4 5 | { "_id" : ObjectId("5d9f7def2e314a2b6c712320"), "name" : "John", "age" : 21, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712321"), "name" : "Sam", "age" : 25, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712322"), "name" : "Lisa", "age" : 33, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712323"), "name" : "Max", "age" : 31, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712324"), "name" : "Ronn", "age" : 25, "__v" : 0 } |
For testing this route, we will use the postman too. You can download the postman tool from www.getpostman.com.
Updating a single value in a document
This is the basic function of the update() method. Let’s see how can we update a value in a document.
1 2 3 4 5 6 7 8 9 | router.route("/update").put(function(req, res) { detail.update({ name: "Lisa" }, { name: "Leena" }, function(err, result) { if (err) { res.send(err); } else { res.send(result); } }); }); |
The update() method has three parameters – an object that contains the condition, an object that contains the update, and the callback function. This callback function has two parameters – an error(if any occurs) and the returning value.
Here we are looking for a document where the name is “Lisa”. This route will update the name field of this document to “Leena”. Let’s execute this route using the postman tool and see what is the output.
The update() method returns an object that contains three fields.
“n”: 1 – This means that one document was matched. “nModified”: 1 – This means that one document was updated. “ok”: 1 – This means, that operation was successful.
Let’s check the details collection.
1 2 3 4 5 6 | > db.details.find() { "_id" : ObjectId("5d9f7def2e314a2b6c712320"), "name" : "John", "age" : 21, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712321"), "name" : "Sam", "age" : 25, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712322"), "name" : "Leena", "age" : 33, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712323"), "name" : "Max", "age" : 31, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712324"), "name" : "Ronn", "age" : 25, "__v" : 0 } |
Yes, we can see the name “Lisa” is updated to “Leena”.
When multiple documents can match the condition
What do you think will happen with such a case? Let’s try to update the age field to 50 where age is 25 currently. We can see, there are two documents where age is 25.
1 2 3 4 5 6 7 8 9 | router.route("/update").put(function(req, res) { detail.update({ age: 25 }, { age: 50 }, function(err, result) { if (err) { res.send(err); } else { res.send(result); } }); }); |
Let’s see what is returned this time.
The object indicates that only one document was matched and updated. But wait! we had two documents where age was 25. Let’s check the details collection first.
1 2 3 4 5 6 7 | > db.details.find() { "_id" : ObjectId("5d9f7def2e314a2b6c712320"), "name" : "John", "age" : 21, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712321"), "name" : "Sam", "age" : 50, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712322"), "name" : "Leena", "age" : 33, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712323"), "name" : "Max", "age" : 31, "__v" : 0 } { "_id" : ObjectId("5d9f7def2e314a2b6c712324"), "name" : "Ronn", "age" : 25, "__v" : 0 } > |
We can see only the first document where age was 25 is updated. The second one is ignored. This is what the update() method does. It only updated the very first matched document.
Conclusion
The update() method updates only the first matched document. All other documents that can match are ignored. This is one of the reasons why the update() method is deprecated. Check the console when this method is used.
These methods can be used according to the requirement and the update() method should be avoided.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started