Last active
September 1, 2017 12:14
-
-
Save sideshowcoder/10d31f565dca4358ef1f to your computer and use it in GitHub Desktop.
Mass insert documents in Couchbase
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 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