1

I'm currently trying to display a query in which I'm trying to sort by a specific column ("epoch_start").

For some reason, I'm able to display the query by using the find() function but whenever I try to use the sort() function, I receive the following error:

Uncaught Error: Call to undefined method MongoDB\Driver\Cursor::sort()

Please find my code below:

<?php

$query2 = array('complete_title' => array('$regex' => 'andrew_marr'));

$cursor = $collection_programs
->find($query2)
->sort(array("epoch_start" => 1));

foreach ($cursor as $doc) {

?>

<tr>
  <td><?php echo $doc["pid"] ?></td>
  <td><?php echo $doc["start_time"] ?></td>
  <td><?php echo $doc["end_time"] ?></td>
  <td><?php echo $doc["complete_title"] ?></td>
  <td><?php echo $doc["media_type"] ?></td>
  <td><?php echo $doc["masterbrand"] ?></td>
  <td><?php echo $doc["service"] ?></td>
   <td><?php echo $doc["masterbrand"] ?></td>
</tr>

<?php
}
 ?>

Please can somebody advise?

1
  • Is this a typo question? Is $regex meant to be single quoted? In other words, do you mean to pass the literal string: dollar sign followed by the word regex? Commented Jan 4, 2020 at 3:48

1 Answer 1

1

I haven't used PHP with MongoDB for the last year but there you have an error because you use sort in a cursor component and the documentation said that you can use sort as an option in query component.

Try with this code, I didn't check but if you are using MongoDB Driver maybe work fine.

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');

$filter = ['complete_title' => ['$regex' => 'andrew_marr']];
$options = ['sort' => ['epoch_start' => 1]];
$query = new MongoDB\Driver\Query( $filter, $options );

$cursor = $manager->executeQuery("your_collection", $query);

foreach($cursor as $document) {
    print_r($document);
}

Another option that I just read

$query = new MongoDB\Driver\Query( $filter );

$cursor = $manager->executeQuery("your_collection", $query);
$cursor->sort(['epoch_start' => 1]);

foreach($cursor as $document) {
    print_r($document);
}

Also, you can use the code without filter and check if the results are ordered.

Regards!

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

2 Comments

Late to the party here, the documentation on MongoDB\Driver\Query doesn't seem to specify exactly how the sort option should be used. In the documentation I see examples with ['sort' => ['key' => -1]] stating that it will sort on the key in descending direction. In your example you use 1 instead of -1. Does 1 mean ascending order? Does it accept any other values than 1 and -1?
Hello, you can use 1(asc) or -1(desc) when you want to define the order. MongoDB sort operator only mentions these two options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.