Updating multiple documents in collection with array of ids?

#1

Hey there, not sure if this is possible but I’m having issues updating multiple documents in collection at once.

I am trying:

Meteor.methods({ 
    updateCall(idtoupdate) {
        Tasks.update({ _id:{$in: idtoupdate}, $set: { important: true } });
    }
});

The ‘idtoupdate’ is an array of id’s I would like to update. I’m getting the error ‘invalid modifier, modifier must be an object’

Is there any way to update multiple documents based on their id’s from an array?

Thank you

#2

Try this:

updateCall(idtoupdate) {
  Tasks.update({ _id:{$in: idtoupdate} }, { $set: { important: true } });
}
#3

Unfortunately it didn’t add ‘important: true’ to all the documents selected from the array.

I20190216-23:41:18.183(7)? [ ‘oSHykBHFxZ67LYRxQ’,
I20190216-23:41:18.185(7)? ‘kDPqqPqQqqDbBdQ5q’,
I20190216-23:41:18.187(7)? ‘x5xR5YxXemADbDY97’,
I20190216-23:41:18.189(7)? ‘MninjL55gSxvSscQs’ ]

These are the id’s in the array, only the last ‘MninjL55gSxvSscQs’ was updated with ‘important: true’

#4

You need to use the {multi: true} option:

updateCall(idtoupdate) {
  Tasks.update(
    { _id:{$in: idtoupdate} },
    { $set: { important: true } },
    { multi: true }
  );
}
2 Likes
#5

Multi did it! Thank you so much