MongoDB mapreduce how to split the emit parameter?

Viewed 11

Suppose I want to show the highest purchase based on category using the mapreduce function

db.ecommerce.mapReduce( 
    function(){emit(this.category_code, 1);},
    
    function(key, values)
    {
        return Array.sum(values)
    },
    {
        query:{event_type:'purchase'},
        out: "purchase"
    }
)
db.purchase.find().pretty().sort({value: -1})

However, my category code data is shown like this: "appliances.environment.water_heater". Where should I put my code to split the category_code? Example: appliances.environment.water_heater --> appliances

$arrayElemAt:[{$split:["$category_code","."]}, 0]
0 Answers
Related