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?
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
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.