Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a field of a document in nano module of nodejs for couchdb

Say that there is a couchdb session opened through nano on node.js

var dbserv = require('nano')('http://localhost:5984');

At the couchdb server dbserv can access, there is a database users with users that have a field groups that is an array.

If I wanted to update groups on a user jim in users, how would I do so without replacing the entire document?

like image 800
Havvy Avatar asked Nov 27 '25 19:11

Havvy


1 Answers

CouchDB

To create an update handler, write a design document:

{
  "_id": "_design/yourapp",
  "updates": {
    "foo": "function(doc, req) {
      doc.groups.push(req.query.bar); // or do whatever you like with it
      return [doc, 'done'];
    }"
  }
}

and PUT it in your db with the id _design/yourapp, then GET it like this:

http://localhost:5984/users/_design/yourapp/_update/foo/jim?bar=baz

nanocouch

var dbserv = require('nano')('http://localhost:5984');
var db = dbserv.use('users');

var designdoc = {/* The above design document */};

db.insert(designdoc);

db.get('_design/yourapp/_update/foo/jim', {bar: 'baz'});

Note that you need to insert the design document only once, you can even do it manually using curl, then to update your docs just make a GET request as explained above.

Disclaimer: untested and I never used nano before, but it should be on the lines of what you have to do.

like image 156
Simon Avatar answered Nov 29 '25 08:11

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!