Answer the question
In order to leave comments, you need to log in
How to work with arrays in mongoose?
how to create a schema with an array and then update the array?
I wrote like this, it seems like I can get it, but how can I update it?
xo.update gives error
Answer the question
In order to leave comments, you need to log in
There are a couple of options
. We get the document we need, update - save
1:
const xo = await XO.findById(id);
xo.board.push(10);
await xo.save();
await XO.update({ _id: id }, { $push: { board: 10 } })
Through update, it is possible, as written above, through $push, but as we know, it only adds a new element to the array,
It is also possible through { $set: { "array.$.value" : "" } } but for this you need to make an exact selection to example -
db.board.update({"array.value" : "5"), { $set: {"array.$.value": "15"} })
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question