The collection I have is like:
[{
"day": 10,
"orders": [{
"id": 1,
"items": {
"uuid1": {
"name": "item1",
"status": false
},
"uuid2": {
"name": "item2",
"status": true
},
"uuid3": {
"name": "item2",
"status": false
}
}
}]
},
{
"day": 11,
"orders": [{
"id": 1,
"items": {
"uuid1": {
"name": "item1",
"status": false
},
"uuid2": {
"name": "item2",
"status": true
},
"uuid3": {
"name": "item2",
"status": false
}
}
}]
}]
I would like to delete items with status marked as true for day 10. Tried the one below:
db.<collection>.update (
{day: 10},
{ $pull: {
orders: {items: { status: 'true'} } }
},
{multi: true}
);
The result is:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
and so it doesn't delete.
How should we rewrite this update and pull query so that the items of day 10 marked with status as true are deleted?
CodePudding user response:
You could write a JavaScript to achieve this but might depend on the size of the collection.
db.<collection>.find().forEach((doc)=>{
// doc your work on to the doc and upsert the doc
});
You could try with select documents with a criteria to the find query to ensure everything is in place, before running it against all documents.
CodePudding user response:
It was much more easier to iterate via cursor, update the required collection and save it to DB.