I'm trying to insert multiple records into couchbase, just now, I'm using the official node-couchbase driver.
var db = new couchbase.Connection({host: hostname, bucket: myBucket, password: pass}, function(error){
console.log(error);
});
var g = guid.raw()
var a = [];
for(var i=0; i<100; i++){
new_beer = {
"iteration" : i,
"category": "North American Ale"
}
a.push(new_beer);
}
console.log(guid);
db.set(g, a, function(err, result) {
console.log(err);
});
Just in the insertion, only insert 1 element, I think that is because the g (the guid value is the same for all the registers. How I can insert these 100 registers in 1 only request?
First of all you should generate one guid per one item:
var docs = {};
for(var i=0; i<100; i++){
var guid = guid.raw();
docs[guid] = {
"iteration" : i,
"category": "North American Ale"
}
}
Then you can use setMulti (see docs here) to store all your data in one request.
db.setMulti(docs, {}, function(err, results) {
if (err) throw err;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With