I am stuck at a point where i need to sort the documents based on the date time field following is my database structure
"_id" : ObjectId("59199bbb05b505777d86e9a2"),
"Email" : "[email protected]",
"BlogPosts" : [
{
"PostID" : 7,
"Title" : "Party Time",
"Name" : "Sagar Khan",
"Description" : "Farewell party with 2k17 batchmates at atmosphere 4... #AllNight #Fun #CHEERS....",
"Date" : ISODate("2017-05-15T13:18:01Z"),
"Image" : "users/[email protected]/pictures/prof-pic.jpg",
"Likes" : [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
],
"Comments" : [
{
"CommentID" : 4,
"email" : "[email protected]",
"Name" : "Harish Shinde",
"Image" : "users/[email protected]/pictures/prof-pic.jpg",
"comment" : "Yo... Cheers ",
"Time" : ISODate("2017-05-15T13:18:40Z")
}
]
}
]}
{
"_id" : ObjectId("59450ce02aa01e3027df57be"),
"Email" : "[email protected]",
"BlogPosts" : [
{
"PostID" : 2,
"Title" : "At Goa",
"Name" : "Harish Shinde",
"Description" : "Relaxing at baga Beach Goa ",
"Date" : ISODate("2017-06-17T11:05:20Z"),
"Image" : "users/[email protected]/pictures/prof-pic.jpg",
"Likes" : [
"[email protected]"
],
"Comments" : [ ]
}
]
}
Now i want to sort the documents in descending order based on Date in the BlogPosts array. In Mongodb Console i tried
db.Timeline.find().sort({BlogPosts.Date : -1 }).pretty()
But In PHP i am not able to do this I tried
$cursor = $collection->find()->sort(array("BlogPosts"=>array("Date"=> -1)));
and
$cursor = $collection->find()->sort(array("BlogPosts.$.Date"=> -1 ));
I also tried the solution mentioned in this answer but no luck... Please help me out
$cursor = $collection->find()->sort(array("BlogPosts.Date"=> -1));