Below is my mongo db document structure
{
"product_json": {
"productId": 1,
"productData": [
{
"productName": "A",
"productDetails": [
{
"manufacturedDate": "2014-08-09",
"name": "A1",
"size": 300
},
{
"manufacturedDate": "2012-08-09",
"name": "A2",
"size": 200
}
]
},
{
"productName": "B",
"productDetails": [
{
"manufacturedDate": "2015-08-09",
"name": "B1",
"size": 300
},
{
"manufacturedDate": "2017-08-09",
"name": "B2",
"size": 200
}
]
}
]
}
}
I need to group by on "manufacturedDate" and apply sorting.Also i dont want the whole document as match.only the matched object(matched productDetails object).
db.collection.aggregate([ { $unwind: "$product_json.productData" }, { $unwind: "$product_json.productData.productDetails" }, { $group: { _id: "$product_json.productData.productDetails.manufacturedDate", data: { $push: "$product_json.productData.productDetails" } } } ])$matchstage after$unwind