Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: update an element of an array of a specific document

I am having a collection of documents called 'company'.

company 1 -

{ 
  _id: '1',
  data:[
    {_id:'11', value: 'emp11'},
    {_id:'12', value: 'emp12'}
  ]
}

company 2-

 { 
  _id: '2',
  data:[
    {_id:'21', value: 'emp21'},
    {_id:'22', value: 'emp22'}
  ]
}

Now I want to update value 'emp11' to 'emp99'. I'm following this approach-

companyModel.findById('1', function(err, company) {
    return company.data.update(
        {_id: '11'},
        {$set: {value: 'emp99'}}
    );
});

I'm able to get the company but after that it's showing an error-

company.data.update is not a function

Please suggest a possible solution.

like image 283
kzrfaisal Avatar asked Jul 06 '18 07:07

kzrfaisal


People also ask

How do you update an array element in MongoDB?

Learn how to update array fields in documents in MongoDB collections. You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[] The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

How can I update multiple documents in mongoose?

Mongoose | updateMany() Function The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition. Installation of mongoose module: You can visit the link to Install mongoose module.

How do you update an array in node JS?

To update the first array element of each document that matches your query, use the positional operator $ . The positional operator $ references the array matched by the query. You cannot use this operator to reference a nested array.


Video Answer


2 Answers

companyModel.update(
  { "_id" : :"1", "data._id": "11" }, 
  { "$set": { "data.$.value": "emp99" }}, 
  function(err, company) {
    console.log(company)
})

There is no need to use findById, you can use this directly. In mongo update you need to search for both the document and the field you want to update. The field you want to update is essential as it is used to locate the position of the array when you use $.

However you can do it as following also :

companyModel.findById('1', function(err, company) {
    for(var i =0; i< company.data.length; i++)
        if(company.data._id === "11")
            break;
    if(i<company.data.length)
        company.data[i].value = 'emp99'
    company.save()
});
like image 97
Prajval M Avatar answered Nov 07 '22 07:11

Prajval M


db.company.update({
    _id: "1",
    data: {
        $elemMatch: {
            value: 'emp11'
        }
    }
}, {
    $set: {
        'data.$.value': 'emp99'
    }
})
like image 32
Rubin Porwal Avatar answered Nov 07 '22 07:11

Rubin Porwal