/ mongodb

MongoDB bulk update and insert

Just a note to myself (kinda) how to perform bulk update in MongoDB. (it took me some time to figure it out)

// OrderId = [1,2,3,4,5,6] 

BulkUpdate(OrderId, function() {
	console.log('done');
});

function BulkUpdate(OrderId, callback) {
    Model.update(
                    { orderid : { $in: OrderId } },
                    { $set: { status: 'complete' } },
                    { multi : true}).exec(callback);
}

Update (2014-09-16)

And here is the bulk insert code example:

// JsonArray = [{id: 1, name: test1}, {id: 1, name: test2}]

Model.collection.insert(JsonArray, function(err, result) {
	console.log('Inserted: ' + result.length);
});

Hope you like it!
Stefan