I want to create a filter experience where I take filter criteria from request params and do the filtering of data using the mongo aggregation framework. Please suggest me a suitable approach.
Let the collection of data be like
{
"material": "silver",
"type": "beads",
"shape": "round",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "mixed",
"type": "jewels",
"shape": "round",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "gold",
"type": "jewels",
"shape": "other",
"_user": "5fda2b717112437f9c06"
},
{
"material": "silver",
"type": "jewels",
"shape": "other",
"_user": "5f9b78bf1130ca88e2e1d"
},
{
"material": "mixed",
"type": "jewels",
"shape": "other",
"_user": "5fda2b717112437f9c706"
},
Let the queried data be _user equals 5f9b78bf1130ca88e2e1d, material equals an array of silver, mixed, type equals an array of beads, jewels & shape equals an array of round
My query should return subsequent counts in each filter category(user, material, shape, type).
data = {
categorizedByUser: [
{
_id: '5f9b78bf1130ca88e2e1d',
count: 2
},
{
_id: '5fda2b717112437f9c06',
count: 0
},
],
categorizedByMaterial: [
{
_id: 'silver',
count: 1
},
{
_id: 'mixed',
count: 1
},
{
_id: 'gold',
count: 0
},
],
categorizedByShape: [
{
_id: 'round',
count: 2
},
{
_id: 'other',
count: 0
},
]
categorizedByType: [
{
_id: 'beads',
count: 1
},
{
_id: 'jewels',
count: 1
},
]
]