node.js - How do I update more than one mongodb records at once? -
this question has answer here:
so creating basic todo app using express , mongodb @ backend, have task schema looks :
var taskschema = mongoose.schema({ task: string, time:date, done:boolean, id:number });
by default done not set, in frontend have unordered list , bunch of lines , each line has checkbox next , want when user selects list of tasks checkboxes , click on done button should go post request , mongodb should update tasks @ once , possible? approach correct?
suppose data in db:
[ { _id: "1", task: 'hello1', time: '11-02-2015', done: false }, { _id: "2", task: 'hello2', time: '11-02-2015', done: false }, { _id: "3", task: 'hello3', time: '11-02-2015', done: false }, { _id: "4", task: 'hello4', time: '11-02-2015', done: false }, { _id: "5", task: 'hello5', time: '11-02-2015', done: false } ];
so sending selected tasks (let's call array) via post
request on when done
clicked.
array contain list of taskids this:
var tasksarr = [ "1", "2", "3", "4" ];
so doing in route function..
var bulk = taskschema.collection.initializeunorderedbulkop(); (var taskid in tasksarr) { bulk.find({_id: taskid}).update({ '$set': {done: true} }); } bulk.execute(function(err, bulkres) { console.log('modified:', bulkres.nmodified); });
hope helps.
cheers,
Comments
Post a Comment