node.js - MongoDB update/insert whole collection (array) -
i've got collection of items
,
[ { itemid: 1249, somefield: 'abc' }, { itemid: 1479, anotherfield: 'bcc' } , // etc ]
i recieve portion of data. of items there, not,
[ { itemid: 6534, somefield: 'trw' }, // stored in collection.. { itemid: 1249, somefield: 'abc' } ]
i'm looking way of bulk insert of data upsert
strategy (means, if item such itemid there - update, otherwise insert).
is possible 1 query, of need go throught collection manually , upsert each item?
i use this, not effecent method, works
mongoclient.connect("mongodb://localhost:27017/devbed", {native_parser:true}, function(err, db) { db.collection(project.name).findone({"revision":project.tmprev}, function(err,item){ if(item){ db.collection(project.name).update({json},{w:1},function(err, object) { if (err) {console.warn(err.message);} else {console.log("succesfully saved"); db.close();} }); } else{ db.collection(project.name).insert({json}]},{w:1},function(err, object) { if (err) {console.warn(err.message);} else {console.log("succesfully saved"); db.close();} }); } }); });
wraping in function should work
Comments
Post a Comment