Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 272
Karthik Avatar asked Apr 25 '18 14:04

Karthik


1 Answers

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

like image 141
ANANDA SHERIN Avatar answered Nov 14 '22 16:11

ANANDA SHERIN