So in couchdb using nano if you don't know _rev of the document, the only way to update it is to use db.atomic which needs some design documents to be uploaded to couchdb. Am I right? I would like to know if there is any better way to have the atomic update operation with just doing everything on the code base as opposed to upload these design documents in my couchDB separately as well?
Asked
Active
Viewed 986 times
1 Answers
0
It's really simple to get the _rev of the document, all you need to do is send a HEAD-request and you'll get the rev right back. And Here's some code I found for doing updates in nano:
db.insert({"foo": "bar"}, "foobar", function (error, foo) {
if(err) {
return console.log("I failed");
}
db.insert({foo: "bar", "_rev": foo.rev}, "foobar",
function (error, response) {
if(!error) {
console.log("it worked");
} else {
console.log("sad panda");
}
});
});
I got this from: http://writings.nunojob.com/2012/07/How-To-Update-A-Document-With-Nano-The-CouchDB-Client-for-Node.js.html

Petter
- 1,327
- 11
- 12
-
But this is prone to race condition! – Sep 23 '15 at 19:42
-
That's why I prefer doing my updates in a design document in couchDB, rather then having to bother with _rev:s myself. But this code will likely work, and you can check the response code to see if it's a update conflict, and in that case grab a new _rev and try again. – Petter Sep 23 '15 at 20:39