Can the map collection name be integrated into the key within a MongoDB map operation?

Creating a universal map-reduce function in MongoDB that can be applied to multiple collections. The output will merge results from each collection into one output collection.

Key goals:

  • Include the source collection's name in the key to ensure unique documents in the output collection
  • Avoid hard coding the collection name in the map function for flexibility and code reusability (DRY principle)

Question: Can this be achieved?

Further details below...

Map Function:

function() {
    emit({ cpID: this.cpID, institutionID: this.institutionID, sourceCollection: collName }, 1);
};

Reduce Function:

function(key, values) {
    return values.length;
};

Mongo Shell Commands:

db.loadServerScripts();
var collName;
collName = "activities";
db.activities.mapReduce(mapcpsinstitutions, reducecpsinstitutions, { out: {replace: "cpsAndSchools"}, "scope": { "collName": collName } } );
collName = "activitysummaries";
db.activitysummaries.mapReduce(mapcpsinstitutions, reducecpsinstitutions, { out: {reduce: "cpsAndSchools"}, "scope": { "collName": collName } } );
collName = "contentusagesummaries";
db.contentusagesummaries.mapReduce(mapcpsinstitutions, reducecpsinstitutions, { out: {reduce: "cpsAndSchools"}, "scope": { "collName": collName } } );
collName = "contentusages";
db.contentusages.mapReduce(mapcpsinstitutions, reducecpsinstitutions, { out: {reduce: "cpsAndSchools"}, "scope": { "collName": collName } } );

Answer №1

It appears that your intentions are not entirely clear from the information provided. However, if you wish to maintain a singular "map" function utilizing a "variable" name within the emitted "key," you can make use of the "scope" option for mapReduce:

var colname = "collection";

db.getCollection(colname).mapReduce(
    function() {

       var data = {};

       // Implement map operations here

       emit( collection + idKey, data );

    },
    function(key,values) {
        var reduced = {};

        // Implement reduce operations here

       return reduced;
    },
    { 
        "out": { "merge": "newcollection" },
        "scope": { "collection": colname }
    }
)

Any variables defined in the "scope" section act as global variables accessible to the "map," "reduce," and "finalize" functions without requiring modifications to these functions' code.

The main idea here is to add the current collection name to the beginning of the emitted key.

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

Bootstrap modal experiencing technical difficulties

I am experiencing issues with my bootstrap modal. It seems to be malfunctioning, almost as if the CSS or JavaScript files are not being recognized. I have tried various solutions but have been unable to resolve the problem. I even attempted using the examp ...

Cross-Platform: Varied functionalities in disabled input fields (avoiding any direct replication)

My input HTML field is disabled, but I've noticed that in some browsers (such as Chrome, Edge, Internet Explorer, and Opera), it's still possible to select and copy the text. However, in Firefox, this functionality does not work. To test this yo ...

Is it time to ditch Internet Explorer for EDGE?

Have you ever noticed that when attempting to access the stackoverflow website on Internet Explorer, the tab mysteriously closes and Microsoft Edge opens with stackoverflow loaded? What is the secret behind this strange phenomenon on stackoverflow's ...

Updating data in MongoDB with an expiration feature

Despite setting the expireAfterSeconds option to 60 in my Mongodb shell, my new entries in the unlock schema are not being deleted after 60 seconds. db.unlocks.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 60 } ) Here is the structure of my Sche ...

Click on the links to view various captions for a single image, one at a time

My goal is to create an interactive image similar to what can be found at . (Click Play and navigate to page 5 to see the interactive physical exam). While this example seems to use Flash, I am interested in achieving a similar effect using javascript/jQue ...

Breaking apart a string that consists of boolean values

Presented below is a JavaScript function function cmd_parse( cmd ) { return cmd.split( /\s+/ ); } For instance, when calling the function like this words = cmd_parse("hello jay true"); The output would be words[0]="hello" words[1]="jay" wor ...

Unable to apply programatically loaded attributes to the rangeslider.js plugin

I'm having trouble with my graph editor that uses the rangeslider.js plugin. The bars are not updating properly most of the time, and sometimes they don't work at all. I have a demo available here and a simplified version on Fiddle. Strangely, so ...

Display a concealed div when an ng-click event occurs within an ng-repeat loop

$scope.editPostComment = false; Whenever I click on the button, it displays the textarea in all the repeated items instead of just the clicked div! <div class="commentBox" ng-show = "editPostComment"> <textarea name="editor2" ...

Looking for a way to dynamically adjust the height based on window resizing

I am trying to figure out how to adjust the height of a scrollable grid dynamically. I came across a solution on this website However, the provided solution is not suitable for me as it has: styles: [` html, body, my-app { height: 100%; } '] ...

Insert fresh div elements chronologically using jQuery

When a user clicks on the ".u-post-button" button, this script is triggered. It retrieves the content of the comment input field and posts it to the server as a JSON object. The returned data containing the comment information is then appended after the ...

The modal appears on the screen prior to the content being shown

While attempting to render a bootstrap modal with content from a REST call, I am encountering an issue where the modal appears before the content has finished populating. The modal is triggered by a button click event. If I click the button again after wa ...

Setting state for a dynamic component based on condition in React

Working on creating a dynamic component where the data index matches the URL parameter blogID received from the router. Below are the router parameters sending props to the component: <Route path='/blog/:blogId/:blogTitle' render={() => & ...

Should JavaScript be referenced at the start or generated dynamically?

As I continue working on my small web application, I've noticed that the amount of Javascript is increasing. I'm curious about the best practice for loading/referencing Javascript - should it all be loaded at once at the beginning or dynamically ...

What would be the most efficient method in Angular for saving and retrieving information on whether a user has previously selected a checkbox?

I am currently in the process of learning angular as I develop a web application that resembles a todo list, specifically focused on football teams. In this application, users can navigate through a menu to select a league from four options. The applicatio ...

React Native query: What could be causing the inability to pass parameters (props) to my screen when utilizing stack navigator?

App.js const Stack = createNativeStackNavigator(); export default function App() { return ( <Provider store={store}> <NavigationContainer> <Stack.Navigator initialRouteName="Main"> <Stack.Screen ...

The issue with the AngularJS filter seems to be arising specifically when applied to

My AngularJS filter isn't functioning properly when used with an Object. Here's the View: <input type="text" ng-model="searchKeyUser.$" placeholder="Search Keys" class="form-control"><br> <ul class="list-group"> <li cla ...

Enhanced input field with segmented design, featuring autocomplete and automatic focusing

My current setup involves using the following code snippet: {[1, 2, 3, 4, 5, 6].map((i) => ( <Form.Control key={"code-cell-" + i} className="mx-2 p-0 text-center shadow-none verificatio ...

The ng-change directive in Angular is failing to trigger for checkboxes

I am facing an issue with my code. The ng-change event of individual checkboxes is not triggered when I click the Select All button, but it works fine when I select them individually. I want the itemChecked method to be called when the Select All button is ...

"Contrasting the initialization of state in the constructor with managing state

Can you explain the distinction between these two methods of initializing state in ES6 other than their access to props? constructor(props) { super(props); this.state = { highlighted: 5, backgroundColor: '#f3f3f3', ...

Error: The property 'express-validator#contexts' cannot be read because it is undefined

I'm currently working on validating some data with the express-validator middleware v6.10.1, but I'm encountering an unusual error when testing it with Postman. Here's my code: const { check, validationResult } = require('express-valid ...