how to query from update handler javascript code












2















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










share|improve this question



























    2















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










    share|improve this question

























      2












      2








      2








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










      share|improve this question














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







      javascript couchdb fauxton






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 5:21









      techieChamptechieChamp

      156320




      156320
























          2 Answers
          2






          active

          oldest

          votes


















          1















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






          share|improve this answer































            0














            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






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475123%2fhow-to-query-from-update-handler-javascript-code%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1















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






              share|improve this answer




























                1















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






                share|improve this answer


























                  1












                  1








                  1








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






                  share|improve this answer














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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 1:46









                  natevwnatevw

                  9,51254767




                  9,51254767

























                      0














                      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






                      share|improve this answer




























                        0














                        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






                        share|improve this answer


























                          0












                          0








                          0







                          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






                          share|improve this answer













                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 15:56









                          Alexis CôtéAlexis Côté

                          2,9402824




                          2,9402824






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475123%2fhow-to-query-from-update-handler-javascript-code%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Fotorealismo