Omi
Omi

Reputation: 1148

how to query from update handler javascript code

What I'm trying to do:

  1. I want to add data (id, data) to db. While doing so, I want to check if the id already exists and if so, append to existing data. else add a new (id, data) entry.

Ex: This could be a db of student id and grades in multiple tests. If an id exists in the db already, I want to just append to the existing test scores already in db, else create a new entry: (id, grades)

  1. I have setup an update handler function to do this. I understand, couchdb insert by default does not do the above. Now - how do I check the db for that id and if so, decide to whether to add a new entry or append. I know there is db.get(). However, I presume since the update handler function is already part of the db itelf, there may be a more efficient way of doing it.

I see this sample code in the couchdb wiki:

function(doc, req){
    if (!doc){
        if ('id' in req && req['id']){
            // create new document
            return [req, 'New Document'];
        }
        // change nothing in database
        return [null, 'Incorrect data format'];
    }
    doc[id] = req;
    return [doc, 'Edited World!'];
}

a few clarifications in this example that's not clear: where do we get the id from? Often the id is not explicitly passed in while adding to db.

Does that mean, we need to explicitly pass a field called "_id"?

Upvotes: 2

Views: 319

Answers (2)

natevw
natevw

Reputation: 17932

how do I check the db for that id and if so, decide to whether to add a new entry or append.

CouchDB does this for you, assuming HTTP client triggers your update function using the ID. As the documentation describes:

When the request to an update handler includes a document ID in the URL, the server will provide the function with the most recent version of that document

In the sample code you found, the update function looks like function(doc, req) and so in the code inside of that function the variable doc will have the existing document already "inside" your CouchDB database. All the incoming data from the client/user will be found somewhere within req.

So for your case it might look something like:

function(doc, req){
  if (doc) {
    doc.grades.push(req.form.the_grade);
    return [doc, "Added the grade to existing document"];
  } else {
    var newDoc = {_id:req.uuid, grades:[req.form.the_grade]};
    return [newDoc, "Created new document ("+newDoc._id+") for the grade"];
  }
}

If the client does a POST to /your_db/_design/your_ddoc/_update/your_update_function/existing_doc_id then the first part of the if (doc) will be triggered, since you will have gotten the existing document (pre-fetched for you) in the doc variable. If the client does a POST to just /your_db/_design/your_ddoc/_update/your_update_function, then there is no doc provided and you must create a new one (else clause).

Note that the client will need to know the ID to update an existing document. Either track it, look it up, or — if you must and understand the drawbacks — make them determinate based on something that is known.


Aside: the db.get() you mentioned is probably from a CouchDB HTTP client library and is not availble (and would not work) in the context of any update/show/list/etc. functions that are running sandboxed in the database "itself".

Upvotes: 1

Alexis Côté
Alexis Côté

Reputation: 3690

Cited from the documentation:

If you are updating an existing document, it should already have an _id set, and if you are creating a new document, make sure to set its _id to something, either generated based on the input or the req.uuid provided. The second element is the response that will be sent back to the caller.

So tl;dr, if the _id is not specified, use req.uuid.

Documentation link: http://docs.couchdb.org/en/stable/ddocs/ddocs.html#update-functions

Upvotes: 0

Related Questions