Last active
August 29, 2015 13:56
-
-
Save akilism/9340931 to your computer and use it in GitHub Desktop.
Using Node, Q, and mongodb save an array of objects to a mongo collection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var saveObjects = function (objects) { | |
var MongoC = MongoClient; | |
var mongoConnect = Q.nbind(MongoC.connect, MongoC); | |
return mongoConnect(dbString).then(function (db) { | |
var DocumentCollection = db.collection('collection'); | |
var documentCollectionUpsert = Q.nbind(DocumentCollection.update, DocumentCollection); | |
//Use upsert option. | |
var options = { 'upsert' : true }; | |
var upsertPromises = objects.map(function (obj) { | |
return documentCollectionUpsert(obj, obj, options); | |
}); | |
return Q.all(upsertPromises).finally(function () { | |
db.close(); | |
}); | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment