Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Last active September 1, 2017 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideshowcoder/10d31f565dca4358ef1f to your computer and use it in GitHub Desktop.
Save sideshowcoder/10d31f565dca4358ef1f to your computer and use it in GitHub Desktop.
Mass insert documents in Couchbase
var couchbase = require("couchbase")
var async = require("async")
var db = new couchbase.Connection({})
// create insert function which expects just a callback
function insertFormObject(object) {
return function (cb) {
db.set(object._id, object.value, cb)
}
}
// create array of inserts
var documents = [
{ _id: "mykey-0", value: { iam: "adoc" } },
{ _id: "mykey-1", value: { iam: "adoc" } },
{ _id: "mykey-2", value: { iam: "adoc" } },
{ _id: "mykey-3", value: { iam: "adoc" } }
]
var inserts = documents.map(function(o) { return insertFormObject(o) })
// run all inserts in parallel
async.parallel(inserts, function(err) {
if(err) new Error(err);
db.shutdown();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment