1

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?

6
  • 1
    what is the error? Commented Dec 12, 2018 at 21:56
  • 1
    Your question statement talks about a field called priority_data that is nowhere to be found in your code snippet. Does your schema also have a field called "disk_info"? Commented Dec 12, 2018 at 22:32
  • 1
    Add your schema and you exception please. Commented Dec 12, 2018 at 23:07
  • sorry the array is priority_data Commented Dec 13, 2018 at 15:31
  • when im doing doc.priority_data[1] = 'Office Document'; and after doc.save(); The index 1 of the array is not changed in my database Commented Dec 13, 2018 at 15:43

1 Answer 1

1
await Disk.update(
    {_id: id},
    {$set:{"priority_data.1":"Office Document"}}
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.