My document looks like as shown below and I want to transform it using aggregation. Inside favourite_products I have product array and it has product_id corresponding to each shops. Now I just want product ids of all shops but sorted based on time. :
"favourite_products": [
{
"shop": {
"shop_id": "59465888f7babb000485574b",
"time": "2017-07-12T06:11:19.817Z"
},
"product": [
{
"product_id": "594c2d56f7afcf00043b1195",
"time": "2017-07-12T06:10:36.775Z"
},
{
"product_id": "594ac36c76de720004e819f6",
"time": "2017-07-12T06:11:19.817Z"
}
]
},
{
"shop": {
"shop_id": "593acc24a2902d0004211f1f",
"time": "2017-07-12T06:12:59.372Z"
},
"product": [
{
"product_id": "594ac36c76de720004e819f6",
"time": "2017-07-12T06:12:59.372Z"
}
]
}
]
I want to transform it into this:
"favourite_products"
["59465888f7babb000485574b",594c2d56f7afcf00043b1195","594ac36c76de720004e819f6","593acc24a2902d0004211f1f","594ac36c76de720004e819f6"]
$unwind, and that is something you need to avoid if this really is just output "per document". It's a task better suited to client side code instead. And by "client" that means the application client to the database, as opposed to the "browser" or other eventual client. It's very easy to transform data per document without resorting to the aggregation framework.