Exploring the correlation between aggregation operations in MongoDB and MapReduce techniques

For days, I've been grappling with translating this specific query into MapReduce. My task is to determine the number of different cars that have covered a distance of "N" kilometers.

The Query:

db.adsb.group({
    "key": {
        "KM": true
    },
    "initial": {
        "countCar": 0
    },
    "reduce": function(obj, prev) {
        if (obj.Matricula != null)
            if (obj.Matricula instanceof Array)
                prev.countCar += obj.Matricula.length;
            else
                prev.countCar++;
    },
    "cond": {
        "KM": {
            "$gt": 10000,
            "$lt": 45000
        }
    }
});

Each document in Mongo follows this structure:

{
        "_id" : ObjectId("5a8843e7d79a740f272ccc0a"),
        "KM" : 45782,
        "Matricula" : "3687KTS",
}

My objective is to achieve something like this:

 /* 0 */
 {
     “KM” : 45000,
     “total” : 634
 }

 /* 1 */
 {
     “KM” : 46000,
     “total” : 784
 }

Although my code compiles, it fails to produce the desired output. It appears that each time 'reduce' is entered, all values reset to zero, hindering the accumulation of registrations. One challenge arises when dealing with large datasets as the function must iterate multiple times within 'reduce'. I'm unsure if this method is suitable, or if I should return a list of car plates and their count within 'reduce', then sum them up during 'finalize'.

// Map function
var m = function() {
  if (this.KM > 10000 && this.KM < 45000) { 
    var fl = Math.round(this.KM / 1000) * 1000;
    var car = this.Matricula
    emit (fl, car);
  }
};

// Reduce function
var r = function(key, values) {
    var ya_incluido = false;
    var cars_totales = 0;
    var lista_car = new Array();

    for (var i=0; i < values.length;i++)
    {
            for (var j=0; j < lista_car.length;j++)
            {
                    if(values[i] == lista_car[j]) {
                            ya_incluido = true;
                    }
            } 
            if (ya_incluido != true) {
                    lista_car.push(values[i]);
            } 
            ya_incluido = false;
    }

    cars_totales = lista_av.length; 
    return cars_totales;

};


// Finalize function
var f = function(key,value) {
  
}


db.runCommand( {
                 mapReduce: "dealer",
                 map: m,
                 reduce: r,
                 finalize: f,
                 out: {replace : "result"}
               } );

An insightful answer can be found here:

Answer №1

I recently came across a fantastic answer and explanation at the following link:

In my search, I struggled to figure out how to maintain the values from 'map' when using 'reduce'. It seemed like each iteration was overwriting the previous results. However, the solution provided in the aforementioned link resolved this issue effortlessly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for capturing an error generated by a child component's setter?

I've created an App component that contains a value passed to a Child component using the @Input decorator. app.component.html <app-child [myVariable]="myVariable"></app-child> app.component.ts @Component(...) export class AppC ...

Rendering a Nativescript component once the page has been fully loaded

I'm currently working on integrating the WikitudeArchitectView into my TypeScript code. I've successfully registered the element in the main.ts TypeScript file: var architectView = require("nativescript-wikitudearchitectview"); registerElement ...

"Enhance your client-side PDF capabilities with the pdfMake plugin, offering support for multiple languages

I am in search of a plugin that can convert HTML content into a PDF format and is compatible with javascript/jQuery/AngularJS. It must also provide multilingual support. I have experimented with the following two plugins, but encountered limitations with ...

Combining ng-repeat with manipulating the DOM beyond the scope of a directive

I'm struggling to understand Angular Directives and how they work. I have a simple array of objects in my controller, but creating the desired DOM structure from this data model is proving challenging. Any advice on best practices would be greatly app ...

Upon being provided with a route param, the response will be

For my current project using Express, I want to implement Reddit-like functionality where appending ".json" to any URL will return JSON data instead of the rendered template. To set up the rendering engine in Express, I am using Jade. Here is how I config ...

Generate a vector3 with a defined direction

I have a challenge at hand where I am looking to create a 2D flow field in three.js based on a working example I found in p5.js. The original source code for reference is as follows: var inc = 0.1; //Increment of noise var yoff = 0; var scl = var; //Scale ...

Steps to adding a collection of links (stylesheets, javascript files) to each html document

Is it feasible to add a list of links to multiple HTML files? I was inspired by W3 School's W3 Include functionality, which allows you to import blocks of HTML code into various files simultaneously, making it convenient for making changes across many ...

Add a pair of assorted div elements to a shared container

I have two different sections with classes named "red" and "blue". By default, these sections are hidden. I want to duplicate them and display them in a single container named "cont". The red button appends the red section and the blue button appends the b ...

Zero Clipboard may experience functionality issues if it is invoked dynamically

Copying to the clipboard is made possible with Zero Clipboard, a tool recommended in this answer. The code functions perfectly when used as shown below: <div id="d_clip_button" style="background: #FFFFCC;"> Click to copy </div> <script ...

"Using regular expressions in a MongoDB find() query does not provide the desired

app.get("/expenses/:month", async (req, res) => { const { month } = req.params; const regexp = new RegExp("\d\d\d\d-" + month + "-\d\d"); console.log(regexp); const allExpenses ...

The functionality of AngularJS's state URL depends on numerical URLs for navigation

Currently, I am utilizing the following URL in my state setup: .state('forum.spesific', { url: '/:articleId', templateUrl: 'modules/forum/client/views/forum.client.view.html', controller: 'forumCont ...

React Native - Implementing a dynamic form that adapts based on the answer given by its parent

My JavaScript Object has a simple state structure as follows: pertanyaan: [{ label: "label1", type: 'dropdown', key: 'keyFoo1', option: [{ value: "foo1" }, { value: "foo2", additional ...

Populate a secondary dropdown menu using the selection from a primary dropdown menu and retrieve the corresponding "value" instead of displaying the value as a dropdown option

I am attempting to create two dropdowns that are populated by another dropdown. Below is the code: HTML: <form type=get action="action.php"> <select name="meal" id="meal" onChange="changecat(this.value);"> <option value="" disabled select ...

The regex path matching issue in next.config.js is being caused by the pattern not being able to start with "?"

In my upcoming project, I attempted to utilize regex path matching in the next.config.js file as explained in the documentation. The goal was to match all routes except for one specific route by adding the regex ^(?!.*books). which successfully excludes an ...

Issue with material-ui-dropzone, the DropzoneAreaBase component not displaying the preview of the uploaded files

Has anyone encountered issues with the DropzoneAreaBase component from the material-ui-dropzone library? I am having trouble getting it to display added file previews. Any insights into why this might be happening? <DropzoneAreaBase onAdd={(fileObjs) ...

Utilizing data from a JavaScript array and merging it with basic HTML structure

Every day, a js array source on a remote server gets updated: var io = new Array(); nsi[0] = new Array('','Frank','','Factory worker','Mercedes',374.0,26.2,76,181,'',75,'Audi',1456.5,27 ...

Testing React Hooks in TypeScript with Jest and @testing-library/react-hooks

I have a unique custom hook designed to manage the toggling of a product id using a boolean value and toggle function as returns. As I attempt to write a unit test for it following a non-typescripted example, I encountered type-mismatch errors that I' ...

Using forEach in React to simultaneously set multiple properties and return destructured output within the setState function

The following is the initial code snippet: setRows((rows) => rows.map((row) => selected && row.node === selected.id ? { ...row, row.a: "", row.b: "", row.c: "" } ...

Developing distinct state values for assigned objects

I developed a star rating component using Material UI components that is based on a mapped object. However, I am facing an issue where all the star ratings show the same value when clicked. How can I ensure that each star section displays its own value bas ...

Ways to eliminate the initial digit of a decimal number if it is less than 1

I need assistance with modifying float values by removing the first number if it's lower than 1 In the "OPS" table section, I am calculating the sum of OBP and SLG obtained from a database. See the code snippet below: <td>{props.player.OBP}< ...