php7查询记录数,PHP7查询数据-executeQuery函数

PHP7查询数据-executeQuery函数

发布时间:2020-06-07 04:05:31

来源:51CTO

阅读:1254

作者:素颜猪

// 1.创建数据库连接对象

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

// 2.设置查询条件,返回键,排序规则

$filter = ['index' => ['$gt' => 1]];

$options = [

'projection' => ['_id' => 0],

'sort' => ['index' => -1],

];

// 3.创建查询对象

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

// 4.指定查询的数据库中的集合,查询test库的sites集合

$cursor = $manager->executeQuery('test.sites', $query);

// 循环遍历查询的结果

foreach ($cursor as $document) {

// 调用将对象转换为数组函数

$arr = object2array($document);

var_dump($arr['name']);

}

/**

* 对象转换为数组

* @param  object $object 需要转换的对象

* @return array          转换后的数组

*/

function object2array($object) {

$object =  json_decode( json_encode( $object),true);

return  $object;

}

6fd134e2fda9496f56577e04b64807fd.png

21a3bed83af9173f4516940802249318.png