Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return _rev and _id in CouchDB _update function?

Tags:

couchdb

Is there a way to return the newly created document's _rev and _id field to the client from an _update function?

like image 492
ajsie Avatar asked Jul 12 '11 03:07

ajsie


People also ask

How do I update files in CouchDB?

Open the Fauxton url:http://127.0.0.1:5984/_utils/ You can also update/ change/ edit your document once you created. Click on the edit option (encircled in red). After clicking, you will get a new page where you can edit your entries. After editing click on the save changes tab and your document will be updated.

What is the maximum number of views a document can have?

There is no hard limit on the number of views. There are a few things I would recommend though: First, split up your views among many design documents. My first thought is 1 per user, but you could probably sub-divide them further depending on how many views you actually have.

What are views in CouchDB?

Basically views are JavaScript codes which will be put in a document inside the database that they operate on. This special document is called Design document in CouchDB. Each Design document can implement multiple view. Please consult Official CouchDB Design Documents to learn more about how to write view.


1 Answers

You can, however the solution is not perfect.

You already know the document _id in the update function. Either you are computing it yourself, or using user input, or if you wish to let CouchDB produce an ID automatically, then use the req.uuid value.

function(doc, req) {
    // An example _update function.
    var id;

    id = "Whatever";   // Pick one yourself, or...
    id = req.query.id; // Let the user specify via ?id=whatever, or...
    id = req.body;     // Let the user specify via POST or PUT body, or...
    id = req.uuid;     // Use a random UUID from CouchDB

    var doc = {"_id":id, "other_stuff":"Whatever other data you have"};
    log("Document _id will be: " + doc._id);
    return([doc, {json: {"success":true, "doc":doc}]);
}

Unfortunately, you do not know the _rev in the show function. However, CouchDB will send it to the client in the HTTP header X-Couch-Update-NewRev.

For example:

HTTP/1.1 201 Created
X-Couch-Update-NewRev: 1-967a00dff5e02add41819138abb3284d
Server: CouchDB/1.1.0 (Erlang OTP/R14B03)
Date: Tue, 12 Jul 2011 06:09:34 GMT
Content-Type: application/json
Content-Length: 14

{"stuff":true}
like image 184
JasonSmith Avatar answered Oct 23 '22 11:10

JasonSmith