I have MonogoDB collection described with following MongoEngine document:
class Inner(EmbeddedDocument):
value = StringField()
class Outer(Document):
inner = EmbeddedDocumentListField(Inner)
So db.outer
collection in MongoDB can look like this:
{ "inner": [{ "value": "A" }, { "value": "B" }] },
{ "inner": [{ "value": "B" }] },
{ "inner": [] }
Now, I would like to update all inner.value
, where old value is "B" to "C", so desired result is:
{ "inner": [{ "value": "A" }, { "value": "C" }] },
{ "inner": [{ "value": "C" }] },
{ "inner": [] }
In native MongoDB, I can use this query:
db.outer.updateMany(
{},
{ "$set": { "inner.$[current].value": "C" }},
{ "arrayFilters": [{ "current.value": "B" }]}
)
Is there any way to do that in MongoEngine? And if no, can I somehow run native updateMany
query? I know only about aggregation (Outer.objects.aggregate
).
I found this not very clean (_get_collection should not be public) solution:
Outer._get_collection().update_many(
{},
{"$set": {"inner.$[current].value": "C"}},
array_filters=[{"current.value": "B"}]
)