New to MongoDB here and I am having a hard time achieving this. I have a database with a collection called posts. It has the following structure (in it's simplest form):
{
"_id": ObjectId
"title" : String
"content" : String
"comments" : Array
}
Using PHP with the new MongoDB driver, I wish to run a query that returns documents arranged by the number of comments. I used the following code but I am not sure if it's the right way to do it:
$cursor = $collection->find([],
[
'sort' => [ 'comments' => - 1 ]
]
);
Any help will be much appreciated! Thank you SO community!