On Thu, 14 Nov 2013 13:11:39 GMT, Drasha wrote:
Hi,
I am most likely missing something trivial, but I don't understand what should I do to update an entry in a DB.
I have found two possible methods - update and findAndModify and can't make any of them work. In the mongodb docs there is an example:
db.runCommand( { findAndModify: "people", query: { name: "Tom", state: "active", rating: { $gt: 10 } }, sort: { rating: 1 }, update: { $inc: { score: 1 } } } )
those two functions take query and update parameters. The query part is easy, but I don't know how to translate this code:
`{ $inc: { score: 1 } }
` into the D code.Can you give me a hint?
Thanks,
Drasha
Should be:
collection.update(
[
"name": Bson("Tom"),
"state": Bson("active"),
"rating": serializeToBson(["$gt": 10])
],
["$inc": ["score": 1]]
);
// or
collection.findAndModify(
[
"name": Bson("Tom"),
"state": Bson("active"),
"rating": serializeToBson(["$gt": 10])
],
["$inc": ["score": 1]]
);
The "sort" field is not supported for update
and findAndModify
, though. But it can be done manually using the same runCommand
call as in JS:
auto cmd = Bson.emptyObject;
cmd["findAndModify"] = Bson("people");
cmd["query"] = Bson([
"name": Bson("Tom"),
"state": Bson("active"),
"rating": serializeToBson(["$gt": 10])
]);
cmd["sort"] = Bson(["rating": Bson(1)]);
cmd["update"] = serializeToBson(["$inc": ["score": 1]]);
db.runCommand(cmd);