Following code is not updating book title, how can achieve my goal of updating book title?
user: {
_id: "123",
books: [{ title: "ABC", pages: 99 }],
}
await model.updateOne(
{
_id: userID,
"books._id": bookID,
},
{ book: { title: "" } }
);
CodePudding user response:
From your scenario, you need arrayFilters
.
db.collection.update({
_id: "123" //userID
},
{
$set: {
"books.$[book].title": ""
}
},
{
arrayFilters: [
{
"book._id": "1" //bookID
}
]
})
References
How the arrayFilters Parameter Works in MongoDB
CodePudding user response:
try this
await model.updateOne(
{
_id: userID,
"books._id": bookID,
},
{ title: "" }
);