admin管理员组文章数量:1401673
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?
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?
- 1 You can "emulate" this behaviour using document update handlers. – Marcello Nuccio Commented Jan 24, 2012 at 15:13
- @MarcelloNuccio I saw that page before this question, and it left me confused with how to actually create one, store it, and execute it. And since I'm at a loss, I'm requesting an example. – Havvy Commented Jan 24, 2012 at 19:11
2 Answers
Reset to default 4CouchDB
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.
Found a way to make updates, without all _design needs and such. The issue with Nano CouchDB is that the insert doesn't really provide a place to send the correct _rev that is needed for an update. Anyhow, there is a work around. What one needs to do, is to get the _rev value from the _id you want to update. So you use Nano db.get as the big function, get the _rev value and update your document. Here is a code sample:
users.get('document_name', function(err, doc) {
updaterev = doc._rev;
users.insert({title:'here_ya_go',_rev:updaterev},'document_name', function(err, body , header) {
if (!err)
{
console.log(body);
res.send('update website succeed');
}
else
{
console.log(err.error);
}
});
});
When the db.insert functions are inside db.get function, it gives us a chance to grab hold of the _rev and update out document.
I know, I probably didn't invent this, but it's important to have this piece of code to grab and go. Good luck.
本文标签: javascriptUpdate a field of a document in nano module of nodejs for couchdbStack Overflow
版权声明:本文标题:javascript - Update a field of a document in nano module of nodejs for couchdb - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744313437a2600143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论