Even though the answer given essentially outlines the approach, you can do this sort of thing with MongoDB 2.6 or greater due to the implementation there that supports "bulk updates".
This are still, "atomically speaking", separate update statements. But you can submit them "over the wire" in one go. Which at least makes sure that the latency between the updates is much shorter as they are executed on the server:
var bulk = db.foo.initializeBulkOrderedOp();
bulk.find({ "site_id": "xxx",
"title.de": { "$exists" false } })
.update({ "$set": { "title.de": "" } });
bulk.find({ "site_id": "xxx",
"content.de": { "$exists" false } })
.update({ "$set": { "content.de": "" } });
bulk.execute();
So that is actually one round trip to the server as everything only sends on .execute()
But in your present form (though this may not be a accurate representation of your data), you can actually "re-structure" in order to do this in a single operation. So if your documents looked like this:
{
"site_id": "xxx",
"docs": [
{ "title": "a", "content": "a", "lang": "ru" },
{ "title": "b", "content": "b", "lang": "en" }
]
},
{
"site_id": "xxx",
"docs": [
{ "title": "c", "content": "c", "lang": "ru" },
{ "title": "d", "content": "d", "lang": "de" }
]
}
Then the following works by the rule of $addToSet
where the "set" element would be "unique":
db.foo.update(
{ "site_id": "xxx" },
{ "$addToSet": { "docs": { "title": "d", content: "d", "lang": "de" } } },
{ "multi": true }
)
Or even without the logic there and just checking for presence:
db.foo.update(
{ "site_id": "xxx", "docs.lang": { "$ne": "de" } },
{ "$push": { "docs": { "title": "", "content": "", "lang": "de" } } },
{ "multi": true }
)
Which in that last case would result in this:
{
"_id" : ObjectId("53c936265117367f5ff2038b"),
"site_id" : "xxx",
"docs" : [
{
"title" : "a",
"content" : "a",
"lang" : "ru"
},
{
"title" : "b",
"content" : "b",
"lang" : "en"
},
{
"title" : "",
"content" : "",
"lang" : "de"
}
]
}
{
"_id" : ObjectId("53c936265117367f5ff2038c"),
"site_id" : "xxx",
"docs" : [
{
"title" : "c",
"content" : "c",
"lang" : "ru"
},
{
"title" : "d",
"content" : "d",
"lang" : "de"
}
]
}
So the choice is there to either "handle" things differently or otherwise just change your schema to accommodate the sort of updates you want to do atomically.