How to use MapReduce function

Hey everyone,

I’m wondering if anyone has been able to use MapReduce functions in Meteor, I’m working on migration project and using MapReduce is essential as its more flexible than Aggregations “For my project at least”
Here is what I have tried, and for some reason it always give me empty results :roll_eyes:

I have seen number of ancient MapReduce packages (unmaintained), therefore I’m trying with rawCollection()

        const collection = Meteor.users.rawCollection()
        const runCommand = Meteor.wrapAsync(collection.runCommand, collection)

        var map = function() {
            return (this._id, this);
        }

        var reduce = function(previous, current) {
            return current;
        }

        const results = runCommand({ 
            mapReduce: collection,
            map: map,
            reduce: reduce,
            out: {inline: 1},
            query: {roles: "user"},
            sort: { },
            inputDB: "meteor",
         });

I appreciate if anyone can share with me a simple working example.

runCommand is a MongoDB cli method. You probably want to use collection.mapReduce instead.