更新现有文档字段,而不会丢失Couch DB中的旧文档数据

时间:2018-04-25 14:28:19

标签: javascript database nosql couchdb pouchdb

这是我的Couch DB文件

{"people": [{"id": 1,"dept_id": 1,"user": "A",},{"id": 2,"dept_id": 1,"user": "B",},{"id": 3,"dept_id": 1,"user: "C",}]}

用户A将他的dept_id更改为小袋DB中的3

{"people": [{"id": 1,"dept_id": 3,"user": "A",},{"id":2 ,"dept_id": 1,"user": "B",},{"id": 3,"dept_id": 1,"user": "C",}]}

用户B将他的dept_id更改为小袋DB中的4

{"people": [{"id": 1,"dept_id": 1,"user": "A"},{"id":2 ,"dept_id": 4,"user": "B"},{"id": 3,"dept_id": 1,"user": "C"}]}

如果A和B将数据复制到Couch DB,则使用

更新Couch文档
  

{" people":[{" id":1," dept_id":1," user":" A"},{" id":2," dept_id":4,"用户":" B"},{& #34; id":3," dept_id":1,"用户":" C"}]}

但我的预期输出

  

{" people":[{" id":1," dept_id":3," user":" A"},{" id":2," dept_id":4,"用户":" B"},{& #34; id":3," dept_id":1,"用户":" C"}]}

我试过这个来更新小袋Db

var mydb = new PouchDB('localPouchDb',{revs_limit: 1, auto_compaction: true});db.upsert('people ', function myDeltaFunction(doc) { doc.dept_id=4  return doc;}).then(function () {  console.log('success')}).catch(function (err) { console.log(err)});

我试过这个用沙袋来复制沙发Db

var remoteDB = new PouchDB('remote address',{revs_limit: 1, auto_compaction: true});db.replicate.to(remoteDB);

我搜索了一些链接,他们说

  

无法更新单个字段,复制替换   整个文件与囊db

https://www.tutorialspoint.com/couchdb/couchdb_updating_a_document.htm

How to update a document's record/field in couchdb

1 个答案:

答案 0 :(得分:1)

您无法做到。因为CouchDb允许将数据另存为JSON记录。因此,最后更新的记录将添加到bedDB中。

如果要针对特定​​功能执行此操作,请将其作为单独的文档