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 } } );