How To Update An Array In Mongoose
Introduction
Working with arrays can be complicated. Updating tin exist more complicated. 1 should work advisedly while performing update operations on arrays. Mongoose provides a few operators to update arrays such equally $addToSet, $push, $pop, etc. In this commodity, we will discuss how to use such operators to perform update operations on arrays in nodejs.
Offset we want to use a fix of data to demo with. We will use the details drove shown hither:
1 | { "_id" : ObjectId( "5e158429a7b6505cd15e8097" ) , "proper name" : "John" , "historic period" : 21 , "locations" : [ "New York" , "Chicago" ] } |
Nosotros will use the postman tool to perform endpoint testing. You can download the postman tool from www.getpostman.com.
As we will perform an update operation, we accept to make a PUT request. The following road handler will be invoked when the endpoint '/update' is executed.
NOTE Nosotros created our routes using a great packet for NodeJS called limited.
1 | router.road ( "/update" ).put ( function (req, res) { } ) ; |
Let'due south discuss how to update the array using mongoose operators in nodejs.
$addToSet
The $addToSet operator adds values in an array if they are not already present in it. Nosotros specify an assortment of values and it inserts only those values that are not present in it. Allow's add a few values to the locations array where the proper noun is "John".
1 | router.route ( "/update" ).put ( office (req, res) { |
Every bit of at present, the document where the value of the name field is "John" has ii values in the locations array – New York and Chicago. In the update operation, we will add iii new values to it using the $addToSet operator – New York, Texas, and Detroit. One of these 3 is already present in the locations assortment. Let's execute it and see what happens.
It returns an object. Here, the value of nModified is ane. This means, ane document was updated. Let'due south cheque through the mongo shell.
Ii values were added – Texas and Detroit. Information technology did not add New York because it was already nowadays. This is how $addToSet works.
$push
The $push operator is somewhat similar to $addToSet operator. The difference is that it adds values to an assortment fifty-fifty if they are already nowadays. Allow's add "New York" where the value of the name field is "John".
ane | router.route ( "/update" ).put ( function (req, res) { |
Let'south execute and run into what happens.
The $push operator added "New York" in the locations assortment even it was already present at that place. This is the divergence betwixt $addToSet and $button operator.
$pop
The $pop operator removes an chemical element from an array, either from starting or the end. We volition remove one element from both sides. The syntax of $popular is quite similar to the $push operator. The merely difference is, instead of an assortment as the value, nosotros either use 1 or -1. If the value specified is 1, the last element will be removed, and if the value is -one, the kickoff element will exist removed. Let's start past removing the last element, i.e. "New York" from the document where the value of the name field is "John".
1 | router.route ( "/update" ).put ( function (req, res) { |
Yep! The last value was deleted. Now, let's remove the very first value, likewise "New York". This time, nosotros have to give -ane as the value.
one | router.route ( "/update" ).put ( role (req, res) { |
Yeah! The commencement value was also deleted.
Conclusion
Cheers for checking out our knowledge base article! We hoped you lot enjoyed this sit-in of some of the functionality we can use to update arrays with mongoose.
Pilot the ObjectRocket Platform Free!
Endeavour Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started
Source: https://kb.objectrocket.com/mongo-db/using-nodejs-and-mongoose-to-update-array-1205
Posted by: lillardseeagentory1982.blogspot.com
0 Response to "How To Update An Array In Mongoose"
Post a Comment