Sum two values in one view separately in CouchDB
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
add a comment |
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
add a comment |
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
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
javascript view mapreduce couchdb couchdb-futon
edited Nov 28 '18 at 22:58
Solaiman
asked Nov 28 '18 at 22:19
SolaimanSolaiman
1241113
1241113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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