Answer the question
In order to leave comments, you need to log in
How to replace array element in mongoDB?
Have mongodb collection
How to replace tablesState with updated one? Now I have another object created in the array, but the old one is not deleted.
const { email, tablesState } = req.body
Users.update({ email }, { $set: tablesState }
, function (err, WriteResult) {
if (err) return handleError(err);
res.status(201).json(WriteResult)
}
)
Answer the question
In order to leave comments, you need to log in
so replace the array element, or the whole tablesState array?
User.updateOne(
// нужен айдишник для элемента, чтобы его выбрать из базы
{ email: email, 'tablesState._id': table_id },
// тогда можно будет так
{ $set: { 'tablesState.$' : tablesStateElement } }
)
const { email, tablesState } = req.body
Users.findOne({ email })
.then(doc=>{
doc.tableState = tableState;
return doc.save();
})
.then(doc=>res.json(doc))
.catch(err=>handlerError(err);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question