views:

511

answers:

1

I was surprised to find that the following example code only updates a single document:

> db.test.save({"_id":1, "foo":"bar"});
> db.test.save({"_id":2, "foo":"bar"});

> db.test.update({"foo":"bar"}, {"$set":{"test":"success!"}});

> db.test.find({"test":"success!"}).count();
1

I know I can loop through and keep updating until they're all changed, but that seems terribly inefficient. Is there a better way?

+2  A: 

Multi update was added recently, so is only available in the development releases (1.1.3). From the shell you do a multi update by passing true as the fourth argument to update(), where the the third argument is the upsert argument:

db.test.update({foo: "bar"}, {$set: {test: "success!"}}, false, true);

mdirolf
Ah, perfect. It's not urgent, so I'll wait for the officially stable release. Thanks!
Luke Dennis