Reputation:
I have a collection named 'profiles_bak' .i'd like to update the every document in the 'profiles_bak'.for a single condition the below mention code is working fine
db.profiles_bak.update(
{$where : "this.value1 > this.value2"},
{$set : {"Gender": "male" }},false,true)
but i have to update based on multiple condition also i have mention my code what i have used so far or else any other approach how i can achieved this.
db.profiles_bak.update( { $and: [
{query: { $where : "this.value1 > this.value2" },
update: { $set : {"Gender": "male" }},false,true},
{query: { $where : "this.value1 < this.value2" },
update: { $set : {"Gender": "female" }},false,true}
]})
Upvotes: 1
Views: 1317
Reputation: 50406
You cannot do that. Whilst you can construct an expression with JavaScript evaluation it cannot "alter" the the chosen result in the "update" block of the statement. Query logic is separated from the update statement block, and that statement block can only do exactly a singular operation based on the query conditions met.
These are "two" update statements. Use the Bulk Operations API to make it more efficient:
var bulk = db.profiles_bak.initializeUnorderedBulkOp();
bulk.find({ "$where": "this.value1 > this.value2" }).update({
"$set": { "Gender": "male" }
});
bulk.find({ "$where": "this.value1 < this.value2" }).update({
"$set": { "Gender": "female" }
});
bulk.execute();
At least that is one request to the server an one reponse.
The .update() in Bulk is always set to "multi".
Upvotes: 1