Search code examples
javascriptarraysmongodbdistinct-values

get a count of array property values in MongoDB


Given data like

[
    { _id:1, data:["abc", "def", "hij", "klm", "nop"]},
    { _id:2, data:["abc", "def", "hij"]},
    { _id:3, data:["abc", "hij"]},
    { _id:4, data:["abc", "def", "hij", "klm", "nop"]},
    { _id:5, data:["def", "hij", "klm"]},
]

I am trying to get a query result like

[
    { "abc": 4 },
    { "def": 4 },
    { "hij": 5 },
    { "klm": 3 },
    { "nop": 2 },
]

where the number is the count of each data property array string value.

I have been working on it from an aggregate pipeline approach, but it seems like there should be an easier way.


Solution

  • You ought to be able to do this with an aggregation pipeline with $unwind + $group. For example:

    db.col.aggregate([{$unwind:"$data"}, {$group: {_id: "$data", data: {$sum: 1}}}])
    

    Returns:

    { "_id" : "nop", "data" : 2.0 }
    { "_id" : "abc", "data" : 4.0 }
    { "_id" : "def", "data" : 4.0 }
    { "_id" : "hij", "data" : 5.0 }
    { "_id" : "klm", "data" : 3.0 }