My Node/Mongo db has a doc with the following data in a collection called 'prices':
[ { _id: 1, price : 10 }, {_id: 2, price : 15 }, {_id: 3, price : 12 }]
...and so on. I now receive an updated doc from a remote site that that looks like this:
[ { _id: 1, price : 12 }, {_id: 3, price : 15 }, { _id: 5, price: 20 } ] (some new some updated).
How can I change my data to:
Thank you in advance.
You don't need to change your data at all, you can tell MongoDB to perform an upsert
for (var i = 0; i < prices.length; i++) {
var p = prices[i];
collection.update({_id: p._id}, {$set{price: p.price}}, {upsert:true, w:1}, function(err, result) {
if (err != null) {
console.log('Price updated!');
}
});
}