Add values to a set, and update existing values in the same query?

I have a collection called recipients, and I want to update a document in the collection with more information.

I do the following

export const Recipients = new Mongo.Collection('recipients');

Recipients.update(recipients._id: recipientsId {$set { gifts: { blah blah blah }});

I get recipientsId from a call back after an earlier insert into the collection.

I get an error that says ‘Unexpected type cast’ for the Recipients.update line.

What am I doing wrong here?

You have an error in the type of arguments you provide as your selector, it should be an object, here’s the correct form:

Recipients.update({recipients._id: recipientsId}, {$set { gifts: { blah blah blah }});

@serkandurusoy Thanks so much.

I actually got it to work by doing this:

Recipients.update(recipientsId, {$set { gifts: { blah blah blah }});

is there any advantage or disadvantage to doing it this way?

1 Like

That’s right, I typed way too fast. So, to sum it up, both are correct:

Recipients.update({_id: recipientsId}, {$set { gifts: { blah blah blah }});

or

Recipients.update(recipientsId, {$set { gifts: { blah blah blah }});

Because the mongodb selector format that’s allowed is either an object or a string, where, if it is a string, it is assumed to be the _id

Awesome! I appreciate it.

1 Like