CRUD 基本操作

基本操作都是只针对KEY进行操作.

插入数据

bucket.insert('xiaoming', {"age":18, "sex": "boy"}, function(err, result) {
    if (err) throw err;
    console.log(result);
});

Web UI 查看结果:

更新或者插入数据

注意这种方式不支持部分更新.部分更新采用N1QL

当KEY 不存在时做插入操作.

bucket.upsert('xiaoming', {"age":18, "sex": "boy", "country": "china"}, function(err, result) {
    if (err) throw err;
    console.log(result);
});

Web UI 查看结果:

更新替换数据

必须是一个已经存在的KEY,否则会报错.

bucket.replace('xiaoming', {"age":18, "sex": "boy", "country": "china", "language": "chinese"}, function(err, result) {
    if (err) throw err;
    console.log(result);
});

Web UI 查看结果:

查询数据

bucket.get('xiaoming', function(err, result) {
    if (err) throw err;
    console.log(result.value);
});js

console结果:

{ age: 18, sex: 'boy', country: 'china', language: 'chinese' }

查询多个KEY数据

bucket.insert('revin', {"age":28, "sex": "boy", "country": "china", "language": "chinese"}, function(err, result) {
    if (err) throw err;
   //console.log(result);
});

bucket.getMulti(['xiaoming',"revin"], function(err, result) {
    if (err) throw err;
    console.log(result);
});

console结果:

{ xiaoming: 
   { cas: CouchbaseCas<1533176100324966400>,
     value: { age: 18, sex: 'boy', country: 'china', language: 'chinese' } },
  revin: 
   { cas: CouchbaseCas<1533176505367986176>,
     value: { age: 28, sex: 'boy', country: 'china', language: 'chinese' } } }

删除数据

bucket.remove('revin', function(err, result) {
    if (err) throw err;
   console.log(result);
});

Last updated

Was this helpful?