Sum two values in one view separately in CouchDB












1















So Assume I have the following two documents in CouchDB:



{
"_id": "197000000002",
"_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
"iyear": "1970",
"region_txt": "North America",
"country": "130",
"region": "1",
"country_txt": "Mexico",
"nkill": "1",
"nwound": "1",

}

{
"_id": "197000000003",
"_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
"iyear": "1970",
"region_txt": "North America",
"country": "130",
"region": "2",
"country_txt": "Mexico",
"nkill": "3",
"nwound": "1"
}


What I want is to SUM all the nkill and nwound values and have something like this:



[1970, "Mexico","North America"]    4, 2


At this moment I am only able to SUM one of the two values or both combined, but that is not what I want.



My Map Function now looks like this returned as result:



function(doc) {
if(doc.nkill >= 1) {
emit([doc.iyear,doc.country_txt,doc.region_txt],parseInt(doc.nkill));
}
}


And I use _sum for the reduce function.



That returns me a key and value like this:



[1970, "Mexico", "North America"]   4









share|improve this question





























    1















    So Assume I have the following two documents in CouchDB:



    {
    "_id": "197000000002",
    "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
    "iyear": "1970",
    "region_txt": "North America",
    "country": "130",
    "region": "1",
    "country_txt": "Mexico",
    "nkill": "1",
    "nwound": "1",

    }

    {
    "_id": "197000000003",
    "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
    "iyear": "1970",
    "region_txt": "North America",
    "country": "130",
    "region": "2",
    "country_txt": "Mexico",
    "nkill": "3",
    "nwound": "1"
    }


    What I want is to SUM all the nkill and nwound values and have something like this:



    [1970, "Mexico","North America"]    4, 2


    At this moment I am only able to SUM one of the two values or both combined, but that is not what I want.



    My Map Function now looks like this returned as result:



    function(doc) {
    if(doc.nkill >= 1) {
    emit([doc.iyear,doc.country_txt,doc.region_txt],parseInt(doc.nkill));
    }
    }


    And I use _sum for the reduce function.



    That returns me a key and value like this:



    [1970, "Mexico", "North America"]   4









    share|improve this question



























      1












      1








      1








      So Assume I have the following two documents in CouchDB:



      {
      "_id": "197000000002",
      "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
      "iyear": "1970",
      "region_txt": "North America",
      "country": "130",
      "region": "1",
      "country_txt": "Mexico",
      "nkill": "1",
      "nwound": "1",

      }

      {
      "_id": "197000000003",
      "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
      "iyear": "1970",
      "region_txt": "North America",
      "country": "130",
      "region": "2",
      "country_txt": "Mexico",
      "nkill": "3",
      "nwound": "1"
      }


      What I want is to SUM all the nkill and nwound values and have something like this:



      [1970, "Mexico","North America"]    4, 2


      At this moment I am only able to SUM one of the two values or both combined, but that is not what I want.



      My Map Function now looks like this returned as result:



      function(doc) {
      if(doc.nkill >= 1) {
      emit([doc.iyear,doc.country_txt,doc.region_txt],parseInt(doc.nkill));
      }
      }


      And I use _sum for the reduce function.



      That returns me a key and value like this:



      [1970, "Mexico", "North America"]   4









      share|improve this question
















      So Assume I have the following two documents in CouchDB:



      {
      "_id": "197000000002",
      "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
      "iyear": "1970",
      "region_txt": "North America",
      "country": "130",
      "region": "1",
      "country_txt": "Mexico",
      "nkill": "1",
      "nwound": "1",

      }

      {
      "_id": "197000000003",
      "_rev": "1-fbe819b01108f30d2e9e96f3fb46eff8",
      "iyear": "1970",
      "region_txt": "North America",
      "country": "130",
      "region": "2",
      "country_txt": "Mexico",
      "nkill": "3",
      "nwound": "1"
      }


      What I want is to SUM all the nkill and nwound values and have something like this:



      [1970, "Mexico","North America"]    4, 2


      At this moment I am only able to SUM one of the two values or both combined, but that is not what I want.



      My Map Function now looks like this returned as result:



      function(doc) {
      if(doc.nkill >= 1) {
      emit([doc.iyear,doc.country_txt,doc.region_txt],parseInt(doc.nkill));
      }
      }


      And I use _sum for the reduce function.



      That returns me a key and value like this:



      [1970, "Mexico", "North America"]   4






      javascript view mapreduce couchdb couchdb-futon






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 22:58







      Solaiman

















      asked Nov 28 '18 at 22:19









      SolaimanSolaiman

      1241113




      1241113
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Use a custom reduce function instead of CouchDB's built-in _sum.



          You may pass an array or object as the value in the reduce function, separately sum the two values, then output the new array/object.



          So that the reduce function can consume an object (for example), the map function should be changed to something like:



          function(doc) {
          if(doc.nkill >= 1) {
          emit([doc.iyear,doc.country_txt,doc.region_txt],
          {nkill: parseInt(doc.nkill), nwound: parseInt(doc.nwound));
          }
          }


          A different way



          After looking over the CouchDB docs, it appears that the strong preference is to return scalar values from the reduce function - or an array. This SO question covers your use case: how to separately sum two different values from the same query.






          share|improve this answer


























          • thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

            – Solaiman
            Dec 3 '18 at 20:29











          • See my update - there was another SO question that appears to handle your use case.

            – tephyr
            Dec 4 '18 at 16:57












          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%2f53528966%2fsum-two-values-in-one-view-separately-in-couchdb%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Use a custom reduce function instead of CouchDB's built-in _sum.



          You may pass an array or object as the value in the reduce function, separately sum the two values, then output the new array/object.



          So that the reduce function can consume an object (for example), the map function should be changed to something like:



          function(doc) {
          if(doc.nkill >= 1) {
          emit([doc.iyear,doc.country_txt,doc.region_txt],
          {nkill: parseInt(doc.nkill), nwound: parseInt(doc.nwound));
          }
          }


          A different way



          After looking over the CouchDB docs, it appears that the strong preference is to return scalar values from the reduce function - or an array. This SO question covers your use case: how to separately sum two different values from the same query.






          share|improve this answer


























          • thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

            – Solaiman
            Dec 3 '18 at 20:29











          • See my update - there was another SO question that appears to handle your use case.

            – tephyr
            Dec 4 '18 at 16:57
















          0














          Use a custom reduce function instead of CouchDB's built-in _sum.



          You may pass an array or object as the value in the reduce function, separately sum the two values, then output the new array/object.



          So that the reduce function can consume an object (for example), the map function should be changed to something like:



          function(doc) {
          if(doc.nkill >= 1) {
          emit([doc.iyear,doc.country_txt,doc.region_txt],
          {nkill: parseInt(doc.nkill), nwound: parseInt(doc.nwound));
          }
          }


          A different way



          After looking over the CouchDB docs, it appears that the strong preference is to return scalar values from the reduce function - or an array. This SO question covers your use case: how to separately sum two different values from the same query.






          share|improve this answer


























          • thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

            – Solaiman
            Dec 3 '18 at 20:29











          • See my update - there was another SO question that appears to handle your use case.

            – tephyr
            Dec 4 '18 at 16:57














          0












          0








          0







          Use a custom reduce function instead of CouchDB's built-in _sum.



          You may pass an array or object as the value in the reduce function, separately sum the two values, then output the new array/object.



          So that the reduce function can consume an object (for example), the map function should be changed to something like:



          function(doc) {
          if(doc.nkill >= 1) {
          emit([doc.iyear,doc.country_txt,doc.region_txt],
          {nkill: parseInt(doc.nkill), nwound: parseInt(doc.nwound));
          }
          }


          A different way



          After looking over the CouchDB docs, it appears that the strong preference is to return scalar values from the reduce function - or an array. This SO question covers your use case: how to separately sum two different values from the same query.






          share|improve this answer















          Use a custom reduce function instead of CouchDB's built-in _sum.



          You may pass an array or object as the value in the reduce function, separately sum the two values, then output the new array/object.



          So that the reduce function can consume an object (for example), the map function should be changed to something like:



          function(doc) {
          if(doc.nkill >= 1) {
          emit([doc.iyear,doc.country_txt,doc.region_txt],
          {nkill: parseInt(doc.nkill), nwound: parseInt(doc.nwound));
          }
          }


          A different way



          After looking over the CouchDB docs, it appears that the strong preference is to return scalar values from the reduce function - or an array. This SO question covers your use case: how to separately sum two different values from the same query.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 3 '18 at 21:34

























          answered Dec 3 '18 at 17:43









          tephyrtephyr

          72011026




          72011026













          • thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

            – Solaiman
            Dec 3 '18 at 20:29











          • See my update - there was another SO question that appears to handle your use case.

            – tephyr
            Dec 4 '18 at 16:57



















          • thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

            – Solaiman
            Dec 3 '18 at 20:29











          • See my update - there was another SO question that appears to handle your use case.

            – tephyr
            Dec 4 '18 at 16:57

















          thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

          – Solaiman
          Dec 3 '18 at 20:29





          thanks for your answer, I tried the map function. It gives me the two values back. But now I have duplicate values and with the _sum values I could atleast sum them up together and group them. Now I don't know how I can do the same with the two values. Do you have any clue what I need to use at the reduce option?

          – Solaiman
          Dec 3 '18 at 20:29













          See my update - there was another SO question that appears to handle your use case.

          – tephyr
          Dec 4 '18 at 16:57





          See my update - there was another SO question that appears to handle your use case.

          – tephyr
          Dec 4 '18 at 16:57




















          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%2f53528966%2fsum-two-values-in-one-view-separately-in-couchdb%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)