I am trying to sort an inner array by date field, however, I couldn't make it. Here is an example document I have.
{
"_id" : "BTC-ADX",
"parity" : "BTC-ADX",
"data" : [
{
"open" : 0.001,
"high" : 0.002,
"close" : 0.0015,
"low" : 0.0012,
"date" : ISODate("2018-03-16T05:00:00Z")
},
{
"open" : 265,
"high" : 397,
"low" : 190,
"close" : 125,
"date" : ISODate("2018-03-16T06:00:00Z")
},
{
"open" : 265,
"high" : 397,
"low" : 190,
"close" : 125,
"date" : ISODate("2018-03-16T07:00:00Z")
},
{
"open" : 265,
"high" : 397,
"low" : 190,
"close" : 125,
"date" : ISODate("2018-03-16T06:30:00Z")
}
]
}
Additionally, I have an index like;
{
"v" : 1,
"unique" : true,
"key" : {
"parity" : 1,
"data.date" : -1
},
"name" : "parity_1_data.date_-1",
"ns" : "crypto.stockentities"
}
And this is the query I am running
db.stockentities.find({_id:"BTC-ADX"}).sort({ parity:1, "data.date":-1 }).pretty()
But this query returns the same order I have shared above.
Thanks for your help!