2016-11-24 76 views
1

我的代碼:我怎麼能自動增量文檔ID在對的NodeJS Couchbase

bucket.insert("docid", jsonVersion, function (err, response) { 
    if (err) { 
     console.log('Failed to save to Couchbase', err);  
     return; 
    } else { 
     res.send('Saved to Couchbase!'); 
    } 
}); 

我怎麼能自動增加文件編號,當我從Web表單的數據。

我正在使用Nodejs和Couchbase。

+0

http://docs.couchbase.com/developer/java-2.1/documents-atomic.html – Hackerman

回答

1

您在Couchbase中沒有AutoIncrement。 你所擁有的是Counters。 使用計數器可以在原子事物中增加數字,然後將其分配或連接到您的docId。

喜歡的東西:

bucket.counter(counterKey, 1, {initial: 0}, function(err, res) { 
    if (err) throw err; 
    // insert code here 
    console.log('Incremented Counter:', res.value); 

    console.log('Example Successful - Exiting'); 
    process.exit(0); 
    }); 

更多的例子請看這裏: https://github.com/couchbaselabs/devguide-examples/tree/master/nodejs