在不丢失 Couch DB 中的旧文档数据的情况下更新现有文档字段

Updating an existing document field without losing old document Data in Couch DB

这是我的 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 更改为 3 in pouch DB

{"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 更改为 4 in pouch DB

{"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,"user": "B"},{"id": 3,"dept_id": 1,"user": "C"}]}

但我的预期输出

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

我试过这个来更新 pouch 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);

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

it is not possible to update single field , replicate replace the whole document with pouch db

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

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

你做不到。因为 CouchDb 允许将数据保存为 JSON 条记录。所以最后更新的记录将添加到 couchDB 中。

如果您想为您的特定功能做这件事,请将其作为单独的文档