查找和排序查询PHP MONGODB
我目前正试图显示一个查询,其中我正试图按特定列(“epoch\u start”)进行排序 出于某种原因,我可以使用find()函数显示查询,但每当我尝试使用sort()函数时,我都会收到以下错误: 未捕获错误:调用未定义的方法MongoDB\Driver\Cursor::sort() 请在下面找到我的代码:查找和排序查询PHP MONGODB,php,mongodb,sorting,find,Php,Mongodb,Sorting,Find,我目前正试图显示一个查询,其中我正试图按特定列(“epoch\u start”)进行排序 出于某种原因,我可以使用find()函数显示查询,但每当我尝试使用sort()函数时,我都会收到以下错误: 未捕获错误:调用未定义的方法MongoDB\Driver\Cursor::sort() 请在下面找到我的代码: <?php $query2 = array('complete_title' => array('$regex' => 'andrew_marr')); $cursor
<?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
}
?>
有人能给我一些建议吗?我去年没有在MongoDB中使用PHP,但是有一个错误,因为您在游标组件中使用了排序,文档中说您可以在查询组件中使用排序作为选项 尝试使用这段代码,我没有检查,但如果您使用的是MongoDB驱动程序,可能工作正常
$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);
}
我刚刚读到的另一个选项
$query = new MongoDB\Driver\Query( $filter );
$cursor = $manager->executeQuery("your_collection", $query);
$cursor->sort(['epoch_start' => 1]);
foreach($cursor as $document) {
print_r($document);
}
此外,您可以使用不带过滤器的代码,并检查结果是否有序
问候 这是一个打字错误的问题吗?
$regex
是单引号吗?换句话说,您的意思是传递文本字符串:美元符号后跟单词regex吗?