Maboo
Maboo

Reputation: 425

Updating array element in a collection in Meteor

I'm trying to find and update an element within an array in a mongodb collection in a meteor app.

Every element in the array is an object that has an "_id" attribute, so I'm using mongo's '$' pointer:

Collection.update({things._id: currentThingId},{$set: {things.$.value: aGivenValue}});

However, it keeps throwing me an "Unexpected ." exception, pointing to the line where I use "things**.**_id". I followed mongodb documentation, so any chance meteor has some limitatiob with this mongo functionality?

Upvotes: 0

Views: 4915

Answers (2)

chridam
chridam

Reputation: 103305

You need to enclose the field with quotes when using the dot notation to access an element of an array by the zero-based index position, bearing in mind that the positional $ operator limits the contents of an array from the query results to contain only the first element matching the query document. Thus your final update query should look like:

Collection.update({"things._id": currentThingId},{$set: {"things.$.value": aGivenValue}});

Upvotes: 4

Vincent J
Vincent J

Reputation: 801

If every element in your array is an object with an "_id" attribute, why don't you use Collection.update({_id: currentThingId},{$set:{fieldToSet: aGivenValue}}); where fieldToSet is the name of the attribute you want to set aGivenValue to.

Upvotes: 4

Related Questions