hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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();
    }
}