im trying to get count of documents based on array of element. My collection have documents like...
[
{pid: "p001", items :["apple","bat","cat"]},
{pid: "p002", items :["apple","cat","bat"]},
{pid: "p003", items :["apple","bat","cat","dog"]},
{pid: "p004", items :["apple","bat","cat","dog"]}
]
i write a mongo aggregation query like
db.collection_name.aggregate([
{$group : {_id: "$items", count: {$sum : 1}}},
{$project : { _id: 0, items: "$_id" , count: 1 } }
])
im getting output like
[
{items: ["apple","bat","cat"], count: 1},
{items: ["apple","cat","bat"], count: 1},
{items: ["apple","bat","cat","dog"], count: 2}
]
but i want my output like
[
{items: ["apple","bat","cat"], count: 2},
{items: ["apple","bat","cat","dog"], count: 2}
]
can you help me out of this.....