Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple insert Couchbase with node.js

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?

like image 469
Benjamin RD Avatar asked Apr 08 '26 07:04

Benjamin RD


1 Answers

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;
});
like image 101
m03geek Avatar answered Apr 10 '26 20:04

m03geek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!