'How do I delete nested attributes on arrays within mongodb documents? [duplicate]
I migrated a SQLite db to MongoDB and now I have following documents within my collection:
{
"_id": {
"$oid": "622b4b415659480410d9a930"
},
"id": 1,
"dish": "pizza",
"country": "IT",
"ingredients":[
{"id": 1, "amount": "2", "ing": "morzarella", "foreign_key": 1},
{"id": 2, "amount": "5", "ing": "tomatos", "foreign_key": 1}
]
"preparation": "bake it"
}
Obviously I'd like to remove the "id" and "foreign_key" attributes and I had some success with it:
db.recipes.updateMany({}, {$unset: {"id":1}}) removes the "top level" "id" attributes.
db.recipes.find({"ingredients.id": {$exists: true}}, {"ingredients.id": 1}) finds the nested attributes in the "ingredients" array.
But how do I delete these nested attributes?
db.recipes.deleteMany({"ingredients.id": {$exists: true}}, {"ingredients.id": 1}) removes the whole document in which these attributes exists.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
