MongoDB aggregation Frameworkt to Map-Reduce










0















I'm trying to use Map-Reduce instead of aggregation Framework but I'm really struggling with it I can't find any good tutorials on how to deal with it. Let's say I have an aggregation that return average weight and height of people distinct for female and male.



db.people.aggregate
(

$group:

_id:"$sex",
avgHeight: $avg: $toDouble:"$height",
avgWeight: $avg: $toDouble:"$weight",



)


So i assume that i need to make map fucntion that emits: sex, height and weight so it looks like this:



var mapFunction1 = function() 
emit(this.sex, this.height, this.weight);
;


Then a reduce function that return averege height:



var reduceFunction1 = function(keySex, valuesHeight) 
return Array.avg(valuesHeight);
;


And second reduce function for weight:



var reduceFunction2 = function(keySex, valuesWeight) 
return Array.avg(valuesWeight);
;


And finally I call everything out:



db.people.mapReduce(
mapFunction1,
reduceFunction1,
reduceFunction2,
out: "map_reduce_example"
)


and when I try to run it I get error



2018-11-13T17:34:03.372+0100 E QUERY [js] TypeError: c.out is undefined :
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1135:1
@przyklad.js:12:1
failed to load: przyklad.js









share|improve this question
























  • You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

    – Neil Lunn
    Nov 14 '18 at 0:00















0















I'm trying to use Map-Reduce instead of aggregation Framework but I'm really struggling with it I can't find any good tutorials on how to deal with it. Let's say I have an aggregation that return average weight and height of people distinct for female and male.



db.people.aggregate
(

$group:

_id:"$sex",
avgHeight: $avg: $toDouble:"$height",
avgWeight: $avg: $toDouble:"$weight",



)


So i assume that i need to make map fucntion that emits: sex, height and weight so it looks like this:



var mapFunction1 = function() 
emit(this.sex, this.height, this.weight);
;


Then a reduce function that return averege height:



var reduceFunction1 = function(keySex, valuesHeight) 
return Array.avg(valuesHeight);
;


And second reduce function for weight:



var reduceFunction2 = function(keySex, valuesWeight) 
return Array.avg(valuesWeight);
;


And finally I call everything out:



db.people.mapReduce(
mapFunction1,
reduceFunction1,
reduceFunction2,
out: "map_reduce_example"
)


and when I try to run it I get error



2018-11-13T17:34:03.372+0100 E QUERY [js] TypeError: c.out is undefined :
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1135:1
@przyklad.js:12:1
failed to load: przyklad.js









share|improve this question
























  • You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

    – Neil Lunn
    Nov 14 '18 at 0:00













0












0








0








I'm trying to use Map-Reduce instead of aggregation Framework but I'm really struggling with it I can't find any good tutorials on how to deal with it. Let's say I have an aggregation that return average weight and height of people distinct for female and male.



db.people.aggregate
(

$group:

_id:"$sex",
avgHeight: $avg: $toDouble:"$height",
avgWeight: $avg: $toDouble:"$weight",



)


So i assume that i need to make map fucntion that emits: sex, height and weight so it looks like this:



var mapFunction1 = function() 
emit(this.sex, this.height, this.weight);
;


Then a reduce function that return averege height:



var reduceFunction1 = function(keySex, valuesHeight) 
return Array.avg(valuesHeight);
;


And second reduce function for weight:



var reduceFunction2 = function(keySex, valuesWeight) 
return Array.avg(valuesWeight);
;


And finally I call everything out:



db.people.mapReduce(
mapFunction1,
reduceFunction1,
reduceFunction2,
out: "map_reduce_example"
)


and when I try to run it I get error



2018-11-13T17:34:03.372+0100 E QUERY [js] TypeError: c.out is undefined :
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1135:1
@przyklad.js:12:1
failed to load: przyklad.js









share|improve this question
















I'm trying to use Map-Reduce instead of aggregation Framework but I'm really struggling with it I can't find any good tutorials on how to deal with it. Let's say I have an aggregation that return average weight and height of people distinct for female and male.



db.people.aggregate
(

$group:

_id:"$sex",
avgHeight: $avg: $toDouble:"$height",
avgWeight: $avg: $toDouble:"$weight",



)


So i assume that i need to make map fucntion that emits: sex, height and weight so it looks like this:



var mapFunction1 = function() 
emit(this.sex, this.height, this.weight);
;


Then a reduce function that return averege height:



var reduceFunction1 = function(keySex, valuesHeight) 
return Array.avg(valuesHeight);
;


And second reduce function for weight:



var reduceFunction2 = function(keySex, valuesWeight) 
return Array.avg(valuesWeight);
;


And finally I call everything out:



db.people.mapReduce(
mapFunction1,
reduceFunction1,
reduceFunction2,
out: "map_reduce_example"
)


and when I try to run it I get error



2018-11-13T17:34:03.372+0100 E QUERY [js] TypeError: c.out is undefined :
DBCollection.prototype.mapReduce@src/mongo/shell/collection.js:1135:1
@przyklad.js:12:1
failed to load: przyklad.js






mongodb mapreduce aggregation-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:39







Bartek Wiszenko

















asked Nov 13 '18 at 15:46









Bartek WiszenkoBartek Wiszenko

12




12












  • You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

    – Neil Lunn
    Nov 14 '18 at 0:00

















  • You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

    – Neil Lunn
    Nov 14 '18 at 0:00
















You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

– Neil Lunn
Nov 14 '18 at 0:00





You cannot have more than one reduce function. All your work needs to be done in a single reducer. There also really is no value in doing this outside of a learning exercise. If you were paying attention to what is happening over the past few MongoDB releases, you should realize that ALL server-side JavaScript execution is slowly but surely being removed anyway. So it's not really something "new" worth learning

– Neil Lunn
Nov 14 '18 at 0:00












0






active

oldest

votes











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%2f53284621%2fmongodb-aggregation-frameworkt-to-map-reduce%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53284621%2fmongodb-aggregation-frameworkt-to-map-reduce%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo