banner



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
2
3

{ "_id" : ObjectId( "5e158429a7b6505cd15e8097" ) , "proper name" : "John" , "historic period" : 21 , "locations" : [ "New York" , "Chicago" ] }
{ "_id" : ObjectId( "5e158439a7b6505cd15e8098" ) , "proper noun" : "Sam" , "age" : 24 , "locations" : [ "New York" ] }
{ "_id" : ObjectId( "5e158453a7b6505cd15e8099" ) , "name" : "Lisa" , "age" : 27 , "locations" : [ "Washington DC" , "Texas" ] }

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
2
three
4
five
half dozen
vii
viii
9
x
eleven
12
13

router.route ( "/update" ).put ( office (req, res) {
details.updateOne (
{ name: "John" } ,
{ $addToSet: { locations: [ "New York" , "Texas" , "Detroit" ] } } ,
function (err, result) {
if (err) {
res.send (err) ;
} else {
res.send (result) ;
}
}
) ;
} ) ;

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.

Image from Gyazo

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.

Image from Gyazo

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
2
3
4
5
6
7
8
9
x
11
12
thirteen

router.route ( "/update" ).put ( function (req, res) {
details.updateOne (
{ name: "John" } ,
{ $button: { locations: [ "New York" ] } } ,
function (err, outcome) {
if (err) {
res.send (err) ;
} else {
res.transport (issue) ;
}
}
) ;
} ) ;

Let'south execute and run into what happens.

Image from Gyazo

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
2
3
four
5
vi
7
8
9
ten
11
12

router.route ( "/update" ).put ( function (req, res) {
details.updateOne ( { name: "John" } , { $button: { locations: 1 } } , part (
err,
result
) {
if (err) {
res.send (err) ;
} else {
res.send (result) ;
}
} ) ;
} ) ;

Image from Gyazo

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
2
three
4
5
6
vii
eight
9
ten
xi
12

router.route ( "/update" ).put ( role (req, res) {
details.updateOne ( { proper name: "John" } , { $button: { locations: - ane } } , function (
err,
effect
) {
if (err) {
res.ship (err) ;
} else {
res.send (upshot) ;
}
} ) ;
} ) ;

Image from Gyazo

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel