PHP7查询数据-executeQuery函数

<?php


// 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;

}

QQ截图20181024162123.png

QQ截图20181024162123.png

猜你喜欢

转载自blog.51cto.com/suyanzhu/2308452