//连接mongodb
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
//查询条件
$filter = ['user_id'=>['$gt'=>'0']];//查询条件,userid大于0 也可以为1或者空数组
$options = [
'projection' = [],//这个样子显示全部内容,也可以是'_id'=>0
'sort'=>['user_id'=>-1'],//根据user_id字段排序,1是升序,-1是降序
];
$query = new MongoDB\Driver\Query($filter,$options);//查询请求
$list = $manager->executeQuery('location.box',$query);//执行查询location数据库下的box的集合
foreach($list as $document){
$a[] = $document;
}
//结果$a是一个对象,以下代码修改成一个数组
function oject_array($array){
if(is_object($arr)){
$array = (array)$array;
}
if(is_array($array)){
foreach($array as $key=>$value){
$array[$key] = object_array($value){
$array[$key] = object_array($value);
}
}
}
return $array;
}
$arr = object_array($a);
var_dump($a);