Multi update on huge collection

I’m trying to do this:

    Meteor.users.update({}, {
      $set: {
        someField: 20,
      },
    }, {
      multi: true,
    });

Works fine in development with a small user collection, but doesn’t update in production with hundreds of thousands of users.

Anyone else run into this issue? It may be Mongo specific.

I had a similar problem the other day, where updating a large number of records was unacceptably slow. My solution was to leverage mongo’s bulk operations.

You can read more about bulk operations in mongo’s documentation: https://docs.mongodb.com/manual/reference/method/Bulk.find.update/

    const bulk = Meteor.users.rawCollection().initializeUnorderedBulkOp();
    bulk.find({}).update({
      $set: {
        someField: 20
      }
    });
    Meteor.wrapAsync(bulk.execute, bulk)();

Link back to my original question: [Solved] Updating a collection from an array of records

1 Like

Do you use simpleschema or collection hooks? That forces all bulk updates to be transformed to individual updates so that each updated document can be cleaned individually (or so the proper hook can be called on each individual document).

1 Like

I do use an old version of simpleschema.

Try doing:

    Meteor.users.updateMany({}, {
      $set: {
        someField: 20,
      },
    });

This will skip SimpleSchema. I didn’t actually test it out, so it might require some tweaking. @cereal’s approach would probably also work the same way.