1

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

2
  • Use the same "Dot Notation" form as in the shell. That part is not PHP specific: $cursor = $collection->find()->sort(array("BlogPosts.Date"=> -1)); Commented Jun 19, 2017 at 6:55
  • Thanku soo much that worked... Commented Jun 19, 2017 at 7:20

1 Answer 1

1

Instead of:

sort(array("BlogPosts.$.Date"=> -1 ));

try

sort(array("BlogPosts.Date"=> -1 ));

Refer this question

Sign up to request clarification or add additional context in comments.

2 Comments

Thanku soo much that worked...
Sounds great !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.