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

1

7

This is my Couch DB Document

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

User A Changed his dept_id to 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",}]}

User B Changed his dept_id to 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"}]}

If A and B replicate the data to Couch DB , The Couch Document updated with

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

But my expected Output

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

I have tried this to update the 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)});

I have tried this to replicate the couch Db with pouch by

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

I have searched in some links and they said that

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

Stacy answered 25/4, 2018 at 14:28 Comment(1)
Links are correct, it is not possible to perform partial document updates.Howerton
R
1

You can't do it. Because CouchDb allows save a data as JSON records. So last updated record will be add in couchDB.

If you want to do it for your specific functionality, Make it as separate document

Rife answered 23/5, 2019 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.