<?php 
 | 
  
 | 
/**PHP7.x使用 */ 
 | 
  
 | 
namespace  MongoDB7; 
 | 
  
 | 
use MongoDB\Driver\Manager; 
 | 
use MongoDB\Driver\BulkWrite; 
 | 
use MongoDB\Driver\WriteConcern; 
 | 
use MongoDB\Driver\Query; 
 | 
use MongoDB\Driver\Command; 
 | 
  
 | 
class MongoDb 
 | 
{ 
 | 
  
 | 
    protected $mongodb; 
 | 
    protected $database; 
 | 
    protected $collection; 
 | 
    protected $bulk; 
 | 
    protected $writeConcern; 
 | 
    // db默认配置 
 | 
    protected $defaultConfig = [ 
 | 
        'hostname' => 'localhost', 
 | 
        'port' => '27017', 
 | 
        'username' => '', 
 | 
        'password' => '', 
 | 
        'database' => '' 
 | 
    ]; 
 | 
  
 | 
    public function __construct($collection, $dbconfig) 
 | 
    { 
 | 
        $config = array_merge($this->defaultConfig, $dbconfig); 
 | 
        $mongoServer = "mongodb://"; 
 | 
        if ($config['username']) { 
 | 
            $mongoServer .= $config['username'] . ':' . $config['password'] . '@'; 
 | 
        } 
 | 
        $mongoServer .= $config['hostname']; 
 | 
        if ($config['port']) { 
 | 
            $mongoServer .= ':' . $config['port']; 
 | 
        } 
 | 
        $mongoServer .= '/' . $config['database']; 
 | 
  
 | 
        $this->mongodb = new Manager($mongoServer); 
 | 
        $this->database = $config['database']; 
 | 
        //$this->collection = $config['collection']; 
 | 
        $this->collection = $collection; 
 | 
        $this->bulk = new BulkWrite(); 
 | 
        $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100); 
 | 
    } 
 | 
  
 | 
    public function query($where = [], $option = []) 
 | 
    { 
 | 
        $query = new Query($where, $option); 
 | 
        $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query); 
 | 
         
 | 
        // 返回值是 MongoDB\Driver\Cursor ,需要转化为数据数组,方便逻辑中使用 
 | 
        return json_decode(json_encode($result->toArray()), true); 
 | 
    } 
 | 
  
 | 
    public function count($where = []) 
 | 
    { 
 | 
        $command = new Command(['count' => $this->collection, 'query' => $where]); 
 | 
        $result = $this->mongodb->executeCommand($this->database, $command); 
 | 
        $res = $result->toArray(); 
 | 
        $count = 0; 
 | 
        if ($res) { 
 | 
            $count = $res[0]->n; 
 | 
        } 
 | 
  
 | 
        return $count; 
 | 
    } 
 | 
  
 | 
    public function update($where = [], $update = [], $upsert = false) 
 | 
    { 
 | 
        $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]); 
 | 
        $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern); 
 | 
  
 | 
        return $result->getModifiedCount(); 
 | 
    } 
 | 
  
 | 
    public function insert($data = []) 
 | 
    { 
 | 
        $this->bulk->insert($data); 
 | 
        $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern); 
 | 
  
 | 
        return $result->getInsertedCount(); 
 | 
    } 
 | 
     
 | 
    public function insertbatch($datas = []) 
 | 
    { 
 | 
        foreach ($datas as $data) { 
 | 
            $this->bulk->insert($data); 
 | 
        } 
 | 
        $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern); 
 | 
  
 | 
        return $result->getInsertedCount(); 
 | 
    } 
 | 
  
 | 
    public function delete($where = [], $limit = 1) 
 | 
    { 
 | 
        $this->bulk->delete($where, ['limit' => $limit]); 
 | 
        $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern); 
 | 
  
 | 
        return $result->getDeletedCount(); 
 | 
    } 
 | 
} 
 |