I have an array like this:
[
{
name: "product1",
cat: [
{
title: "cat1"
},
{
title: "cat2"
}
],
price: 123
},
{
name: "product2",
cat: [
{
title: "cat1"
}
],
price: 100
}
]
How do I convert to this:
[
{
_id: "cat1",
items: [
{name: "product1", price: 123},
{name: "product2", price: 100}
]
},
{
_id: "cat2",
items: [
{name: "product1", price: 123}
]
}
]
I know I can use $group operator but I only get _id field, please help me.