I am new to MongoDB. I have a requirement to get the latest updated product for particular date from a array of products. Below is the JSON object of my requirement.
{
"_id": ObjectId("10001"),
"product": [{
"image": "./../../../prod1.html",
"name": "product",
"version": 1,
"updatedDate": "14-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 2,
"updatedDate": "14-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 1,
"updatedDate": "15-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 2,
"updatedDate": "15-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 3,
"updatedDate": "15-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 4,
"updatedDate": "15-03-2017"
}]
}, {
"_id": ObjectId("10002"),
"product": [{
"image": "./../../../prod1.html",
"name": "product",
"version": 1,
"updatedDate": "14-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 2,
"updatedDate": "14-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 1,
"updatedDate": "15-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 2,
"updatedDate": "15-03-2017"
}, {
"image": "./../../../prod1.html",
"name": "product",
"version": 3,
"updatedDate": "15-03-2017"
}]
},
}
I need a query - which will retrieve a product with 14-03-2017 with latest version.
Expected Result is:
{
"_id" : ObjectId("10001"),
"product" : [
{"image" : "./../../../prod1.html","name" : "product","version" : 4,"updatedDate":"15-03-2017"}
]},
{
"_id" : ObjectId("10002"),
"product" : [
{"image" : "./../../../prod1.html","name" : "product","version" : 3,"updatedDate":"15-03-2017"}
]}
It would be highly appreciate some one can help me with the aggregate function to get the expected outcome.