TylerBrock / mongo-hacker

MongoDB Shell Enhancements for Hackers
tylerbrock.github.io/mongo-hacker
MIT License
1.79k stars 235 forks source link

Suggestion: add `set` method as `update(q, {$set:updates})` #151

Closed fmaylinch closed 5 months ago

fmaylinch commented 8 years ago

In case you find it useful, we're using this extension. Just not to forget $set!

DBCollection.prototype.set = function(query, setUpdates) {
    return this.update(query, {$set:setUpdates}, false, true);
};
TylerBrock commented 8 years ago

@fmaylinch that looks interesting, kind of dangerous though with multi on! I don't think this is general enough to apply to a lot of use cases. Perhaps with multi off it would be...

fmaylinch commented 8 years ago

We usually want to update all matching docs (in fact it's very rare that we don't). But I understand; a third arg for multi is a good idea, especially as a double check when updating a single doc (even when the query matches only one).

By the way, your find().update() is also multi, isn't it? For me it's fine, since I never use non-multi. I first check my find to be sure what I'm matching and then I replace find by set (and the things I want to set). El 9 feb. 2016 23:12, "Tyler Brock" notifications@github.com escribió:

@fmaylinch https://github.com/fmaylinch that looks interesting, kind of dangerous though with multi on! I don't think this is general enough to apply to a lot of use cases. Perhaps with multi off it would be...

— Reply to this email directly or view it on GitHub https://github.com/TylerBrock/mongo-hacker/issues/151#issuecomment-182102309 .

TylerBrock commented 8 years ago

You are correct the find().update() chain is also multi by default (so that it behaves more like sql).

Want to put up a pull request for the change?

fmaylinch commented 8 years ago

Yes, I'll try.