Meteor/Mongo: Finding and updating certain elements in a collection

14,327

Solution 1

Of course I figure out how to do this right after posting, and of course it's suggested in the Meteor documentation!

And, of course, it's a simple solution:

collection.update({A: true, B: true}, {$set: {C:false}});

Solution 2

As already suggested in comments, the correct answer is:

collection.update({A: true, B: true}, {$set: {C:false}}, {multi: true});

(At least in pure MongoDB, see there).

Without multi: true it will change only one document matching the criteria.

In Meteor it is a bit more tricky, as you are not allowed to do client-side updates other than by matching it (so no possibility for various criteria, no possibility for multi), see http://docs.meteor.com/#update.

You can iterate over all finds, but it would be better to run such code server-side.

Share:
14,327
ajyang818
Author by

ajyang818

Just starting out in the programming world! Learning Python/Django mainly, with some HTML/CSS and Javascript on the side.

Updated on July 23, 2022

Comments

  • ajyang818
    ajyang818 almost 2 years

    I'm starting off with Meteor and need some help with Mongo. I have a collection of names that I'm displaying on a list and want to be able to update one variable of certain entries in the database based on other criteria. Basically what I want to do is:

    For every entry where characteristic A = true and B = true, change characteristic C to be false.

    So far, I've been trying to figure out how Mongo can handle a "for each" loop over the elements of the collection, and for each element check if conditions A and B hold, and then collection.update(element, {C: false}). This is proving to be a lot more problematic than I thought. I want to do something like this (using dummy variable names):

    for (i = 0; i < collection.find().count(); i++){
        if (collection[i].A===true && collection[i].B===true)
            collection.update(collection[i], {$set: {C: false}});
    };
    

    I've been changing this base code around, but am starting to sense that I'm missing something basic about indexing/how collections work in Mongo. Can you index a collection like this (and if so, is this even the most convenient way to do what I'm trying to do?)?

  • David Wihl
    David Wihl over 11 years
    That works for only the first document matching the selector. You need. collection.update({A: true, B: true}, {$set: {C:false}}, 0,1); for all rows.
  • Rouven Hurling
    Rouven Hurling over 11 years
    @DavidWihl to edit multiple rows you need to set {multi: true} as third parameter instead of 0,1 (see meteor docs, docs.meteor.com/#update). so that would be this: collection.update({A: true, B: true}, {$set: {C: false}}, {multi: true})
  • ajyang818
    ajyang818 over 11 years
    Yep, I think Rouven has the built-in solution to that problem!
  • Koen.
    Koen. about 8 years
    multi: true is now also supported by Meteor: docs.meteor.com/#/full/update