I want to create filters
with Multiple Checkboxes. So, For doing that,I’ve to return Facets
from my Server to Frontend
and array.map
it in Frontend
with checkbox
.
MY Database Structure
Category SmartPhone:
[
{
"_id": "1",
"category":"Electronic",
"title": " Samsung M30s",
"price": 66430,
"filters": [
"type":"SmartPhone" //This "type" field may helps in makin new array.
{
"ram": "2GB"
},
{
"storage": "256GB"
},
{
"sensor": "touch"
}
]
},
{
"_id": "2",
"category":"Electronic",
"title": "Redmi Note 9",
"price": 66430,
"filters": [
"type":"SmartPhone" //This "type" field may helps in makin new array.
{
"ram": "8GB"
},
{
"storage": "128GB"
},
{
"sensor": "Scan"
}
]
}
]
Category Books:
[
{
"_id": "3",
"Category":"Study",
"title": "Origin of Life",
"price": 4800,
"filters": [
"type":"Book" //This "type" field may helps in makin new array. {
"pages": "252"
},
{
"weight": "500g"
},
{
"author": "Darwin"
}
]
},
{
"_id": "4",
"Category":"Study",
"title": "History of Time",
"price": 2400,
"filters": [
"type":"Book" //This "type" field may helps in makin new array.
{
"pages": "152"
},
{
"weight": "180g"
},
{
"author": "Stephen"
}
]
}
]
PROBLEM:
I actually want to create dynamic facets means: Display the facets based on Searched Query or Items.
If the query matched in Multiple Categories then returned its filter
array that contains facets values.
OR
if the query matched in Single categories then just returned its filter
array that contains facets values.
So, for doing that,
From the returned documents of the array, I’ve to $group the array filters
based on its category and then $addToSet the filters array to produce a new array which contains its Document Count to display in frontend
but the problem is there is; I’ve many categories. and I don’t know how many categories matched the searched query and returned.
So, How I make such an aggregation that helps me through this ??
Expected Output is:
IF query is matched in Multiple categories:
[
{
"filters": [
"type":"SmartPhone",
{
"ram": ["2GB","4GB"],
"count": 2 //No. of items present in the "ram" array.
},
{
"storage": ["256GB","128GB","64GB"],
"count": 3 // No.of items present in "storage" array.
},
{
"sensor": ["touch","scan"]
"count": 2 // No.of items present in "sensor" array.
}
]
},
{
"filters": [
"type":"Book",
{
"Pages": ["252","152"],
"count": 2 // No.of items present in "Pages" array.
},
{
"weight": ["180g","500g","620g"],
"count": 3 // No.of items present in "weight" array.
},
{
"author": ["Darwin","Stephen"]
"count": 2 // No.of items present in "author" array.
}
]
},
]
If the query matched in single Category:
[
{
"filters": [
"type":"Book",
{
"Pages": ["252","152"],
"count": 2 // No.of items present in "Pages" array.
},
{
"weight": ["180g","500g"],
"count": 2 // No.of items present in "weight" array.
},
{
"author": ["Darwin","Stephen"]
"count": 2 // No.of items present in "author" array.
}
]
},
]
Any suggestion will be Helpful ** **