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?
Erik
1,2071 gold badge13 silver badges28 bronze badges
-
1what is the error?amir– amir2018年12月12日 21:56:56 +00:00Commented Dec 12, 2018 at 21:56
-
1Your 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"?ack_inc– ack_inc2018年12月12日 22:32:29 +00:00Commented Dec 12, 2018 at 22:32
-
1Add your schema and you exception please.Akrion– Akrion2018年12月12日 23:07:10 +00:00Commented Dec 12, 2018 at 23:07
-
sorry the array is priority_dataM3nBeR SuReS– M3nBeR SuReS2018年12月13日 15:31:34 +00:00Commented 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 databaseM3nBeR SuReS– M3nBeR SuReS2018年12月13日 15:43:07 +00:00Commented Dec 13, 2018 at 15:43
1 Answer 1
await Disk.update(
{_id: id},
{$set:{"priority_data.1":"Office Document"}}
)
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js