I have the following data structure in mongodb
[
{
"id" : "unique id 1",
"timeStamp" : "timeStamp",
"topicInfo" : [
{
topic : "topic1",
offset : "offset number",
time: 1464875267637
},
{
topic : "topic2",
offset : "offset number",
time: 1464875269709
},
{
topic : "topic3",
offset : "offset number",
time : 1464875270849
}
]
},
{
"id" : "unique id 2",
"timeStamp" : "timeStamp",
"topicInfo" : [
{
topic : "15",
offset : "offset number",
time : 1464875271884
},
{
topic : "topic2",
offset : "offset number",
time : 1464875273887
},
{
topic : "topic3",
offset : "offset number",
time : 1464875272848
}
]
}
]
Now I want to find all the entry That has topic called "topic2" and the value of time is maximum compare to other object's in the "topicInfo" array. I also want to sort them by "timeStamp". From the example code the query should return the second object. I am not able to write the query any help would be much appreciated.