i have a collection of category and a collection of products, each product has a categoryId field that has an id of a perticular category. I want to know how many products are there for each category.
const categorySchema = mongoose.Schema({
name: String,
});
const productSchema = mongoose.Schema({
name: { type: String, required: true },
categoryId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' }],
});
categoryCollection = [
{ name: "cat1", _id: id1 },
{ name: "cat2", _id: id2 },
{ name: "cat3", _id: id3 },
{ name: "cat4", _id: id4 },
{ name: "cat5", _id: id5 },
]
productsCollection = [
{
name: "product1",
categoryId: [id1, id2]
},
{
name: "product2",
categoryId: [id1, id3]
}
]
i want the result like this result = [{cat1:2,cat2:1,cat3:1,cat4:0,cat5:0}]
i was trying to do it like first getting all the ids from the category collection and matching them with the categoryId field but it returns only the matched products and i am unable to get the count for each product
ids=[id1,id2,id3,id4,id5]
Prodcut.find({categoryId:{$in:ids}})
2
Answers
So you have the right idea. Once you have all the products, you can simply add another function to parse through the returned documents, and count how many products there are.
If you want the map entirely stored as a single item in the result array (i.e [{id1: 0, id2: 0}]) then do this:
If you want you want them as seperate items in the arr like [{id:1},{id: 2}, etc]
try using lookup in mongo aggregation.
this aggregation will generate response like this
if you want to add any match in category field then you can add it before
the lookup stage.
if you need match in lookup then add after lookup stage