SirBT
SirBT

Reputation: 1698

How do I update a collection using multiple _id's?

I am trying to update a collection based on multiple _id's.

I recieve the _id's in an array format via a Session.get() as below:

 var selectedID = Session.get('selectedItemIDSet');

 console.log("selectedID array contents are: "+selectedID); 

The code above ensures that the selectedID array exists and yields:

selectedID array contents are: LZJKA8S3wYNwHakzE,ikrbCDuttHrwkEcuv

The Query below:

buyList.find({_id:{ "$in": selectedID} }).fetch(); 

Successfully yeilds two Objects!

Now to area am having issues with, how to I update the collection with these two _id's

I have tried with the below code:

var PostedArray = [{PostedBy: Meteor.user()._id }];
buyList.update(_id: selectedID, {$set: {wishListArray: PostedArray} });

...but get error message: Uncaught Error: Mongo selector can't be an array.(…)

Any help would be appreciated.

Upvotes: 0

Views: 126

Answers (1)

ghybs
ghybs

Reputation: 53215

Use the same selector in your update as you have done for your find + specify the multi: true option:

buyList.update({ // selector
  _id: {
    "$in": selectedID
  }
}, { // modifier
  $set: {
    wishListArray: PostedArray
  }
}, { // options
  multi: true
});

Note that your 2 documents will be updated with the same modifier.

Upvotes: 2

Related Questions