Hello i am creating a new Web Application for products selling using MEAN stack and i want to see for every brand (HP,ASUS..) how many pc have 8 Gb , how many PC have 4 GB etc. so this is my code
exports.allcountProduct = async (req, res) => {
try {
const count = await Product.aggregate(
{
"$group": {
"_id": {
$toLower: "$ram"
},
"count": { "$sum": 1 }
}
},
{
"$group": {
"_id": null,
"counts": { "$push": { "k": "$_id", "v": "$count" } }
}
},
{
"$replaceRoot": {
"newRoot": { "$arrayToObject": "$counts" }
}
},
],
)
res.json(count);
}
and what im getting is :
[
{
"4GB": 20,
"8GB": 15,
"16GB": 32
}
]
so im getting is the results of all the brands while what im trying to get is every brand alone like this :
[
{
"brand": "ASUS",
"8GB": 6,
"16GB": 14
}
]
this is an example from the document :
{
"_id":ObjectID( "617b0dbacda6cbd1a0403f68")
"brand": "6178524a5ff14e633aeff1ea",
"SerialNumber": "45454234324",
"ram": "4gb",
},
{
"_id":ObjectID( "617b0dbacda6cbd1a040345")
"brand": "6178524a5ff14e633aeff1ea",
"SerialNumber": "azazz5245454az",
"ram": "8gb",
},
{ "_id":ObjectID( "617b0dbacda6cbd1a0403f68") "brand": "6178524a5ff14e633aeff1ea", "SerialNumber": "45454234324", "ram": "4gb", }, { "_id":ObjectID( "617b0dbacda6cbd1a040345") "brand": "6178524a5ff14e633aeff1ea", "SerialNumber": "azazz5245454az", "ram": "8gb", },