Karthik
Karthik

Reputation: 280

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

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

Upvotes: 7

Views: 516

Answers (1)

ANANDA SHERIN
ANANDA SHERIN

Reputation: 26

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

Upvotes: 1

Related Questions