实例化
$mongo = new \MongoDB\Driver\Manager(‘mongodb://XX.XX.XX.XX:27017’)
//查询
//普通查询
$filter= [
'username'=>$username
];
//大于小于等于查询
if ($operator == 1) {
$tmpCode = '$eq' ;
} elseif($operator == 2) {
$tmpCode = '$gt' ;
} elseif($operator == 3) {
$tmpCode = '$lt' ;
}
$filter['BetAmount'] = [$tmpCode => (double)$price] ;
//条件
$options = [
'projection' => ['_id' => 0], //不显示_id ,此字段用于指定显示或不显示某些字段
'sort' => ['id'=>-1], //排序 -1倒序,1正序
'limit'=>$pageSize , 取多少条数据
'skip'=> ($num-1)* $pageSize // 取第几页数据
];
//执行查询
$result = $mongo->executeQuery('data.ky_gameresult', $query_mongo)->toArray() ;
更新操作
//实例化
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 5000);
$bulk->update(
['id'=>$row['Id']], //更新条件
$insertArr, //更新内容
['upsert' => true,//upsert为true,代表如果没有此条记录就执行插入操作,默认false不插入
'multi'=>true] //multi为true,代表更新找到的所有记录,默认false 只更新找到的第一条记录
) ;
//执行更新
$result = $mongo->executeBulkWrite('data.8900gamedata', $bulk, $writeConcern);