Updating an array item using NodeJS, MongoDB & Monk
mongodb, mongoskin, node.js
Solution
Updating items at a position in an array can be done using the positional $ operator
collection.update(
{ _id: data.id, "photos.name": "photo2" },
{ $set: { "photos.$.data": "yourdata" } }
)
Problem
I have a data set like this: ``` { name : 'Doc Name', photos: [ { name: 'photo1', url: 'http://.....' }, { name: 'photo2', url: 'http://......' } ], etc ... ``` Using Monk https://github.com/LearnBoost/monk how do I update photo2? I can use an index as I am iterating over the fields at the moment. My current attempt below gives me an error, and I can't use a variable for the JSON selector (as in the index). ``` collection.update({_id: data._id}, {photos[i].data: filename}, function(err, updatedata) { }); ```