M3nBeR SuReS
M3nBeR SuReS

Reputation: 19

Update array value a given index (mongoose, mongodb)

In my database, I have in my schema a field: priority_data: [ String ] This is an array of priority data.

Here is an example what that field can contain : "priority_data": ["Photo","Video","Music"]

And I want to update for example only the index 2.

I try to proceed with the following code:

await Disk.findOne({_id: id})
   .then(doc => {
   doc.disk_info[1] = 'Office Document';
   doc.save()
   .then(() => {
   res.status(200).json({
  message: 'ok'
})  
})
})

But that throws me an error.

Can anyone help?

Upvotes: 1

Views: 194

Answers (1)

Sai Reddy
Sai Reddy

Reputation: 67

await Disk.update(
    {_id: id},
    {$set:{"priority_data.1":"Office Document"}}
)

Upvotes: 1

Related Questions