如何用php读取mongodb中的单个记录

8xiog9wr  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(157)

我有一个在PHP和MySQL的经验。在MYSQL中使用查询。*" SELECT * from Tablename" *从表中提取所有记录。
*" SELECT * from Tablename" *

yrdbyhpb

yrdbyhpb1#

您可以使用MongoDB/Driver/Manager来完成此操作,如下所示

<?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$manager->executeBulkWrite('db.collection', $bulk);

$filter = ['x' => ['$gt' => 1]];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['x' => -1],
];

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.collection', $query);

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

?>

相关问题