How to update array value in Mongoose

I want to update the value of the array, but I'm not sure about the correct method of its execution, therefore, in order to try the next method, but not work for me.

My model, a field of children in my model

childrens: { type: Array, default: '' } 

My request

  Employeehierarchy.update({ _id: employeeparent._id} ,{ $set: {"$push": { "childrens": employee._id }} }) .exec(function (err, managerparent) {}); 

Can someone please help me. Thanks.

+7
source share
3 answers

You cannot use $set and $push in the same update expression as nested statements.

The correct syntax for using update statements is:

 { <operator1>: { <field1>: <value1>, ... }, <operator2>: { <field2>: <value2>, ... }, ... } 

where <operator1>, <operator2> can be from any of the update statements listed here .

To add a new element to an array, a single $push statement is enough, for example. you can use the findByIdAndUpdate update method to return the modified document as

 Employeehierarchy.findByIdAndUpdate(employeeparent._id, { "$push": { "childrens": employee._id } }, { "new": true, "upsert": true }, function (err, managerparent) { if (err) throw err; console.log(managerparent); } ); 

Using the original update() syntax

 Employeehierarchy.update( { "_id": employeeparent._id}, { "$push": { "childrens": employee._id } }, function (err, raw) { if (err) return handleError(err); console.log('The raw response from Mongo was ', raw); } ); 

in which the callback function takes arguments (err, raw) , where

  • err is an error if any events
  • raw - full answer from Mongo

Since you want to check the changed document, I would suggest using the findByIdAndUpdate function, since the update() method will not give you the changed document, but just the full result of writing from mongo.


If you want to update the field in the document and add the element to the array at the same time, you can do

 Employeehierarchy.findByIdAndUpdate(employeeparent._id, { "$set": { "name": "foo" }, "$push": { "childrens": employee._id } } { "new": true, "upsert": true }, function (err, managerparent) { if (err) throw err; console.log(managerparent); } ); 

The above will update the name field to "foo" and add the employee ID to the childrens array.

+17
source

can follow this

if childrens contains string values, then the model might look like:

 childrens: [{ type : String }] 

if childrens contains the ObjectId values โ€‹โ€‹of another _id collection and wants to populate, then the model might look like:

 childrens: [{ type : mongoose.Schema.Types.ObjectId, ref: 'refModelName' }] 

No need to use $set , just use $push to insert the value into the childrens array. so the query might look like this:

 Employeehierarchy.update( { _id: employeeparent._id}, {"$push": { "childrens": employee._id } } ).exec(function (err, managerparent) { // }); 
+2
source

It will help me guess

 Employeehierarchy.findOneAndUpdate( { _id:employeeparent._id }, { $set: { "childrens": employee._id }} ) 
0
source

Source: https://habr.com/ru/post/1262407/


All Articles