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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| <?php
namespace api\models;
class Mongo { private $_manager; private $_host; private $_username; private $_password; private $_db; private $_port;
public $lastInsertId = '';
public function connect(array $config) { extract($config); $this->_host = $config['hostname']; $this->_username = $config['username']; $this->_password = $config['password']; $this->_db = $config['database']; $this->_port = $config['hostport']; $auth = $username ? $username . ':' . $password . '@' : ''; $mongo = 'mongodb://' . $hostname . ':' . $hostport;
$extra = [ // 'db' => $config['database'], 'authSource' => $config['authSource'] ?? $config['database'], ]; if ($config['username']) { $extra['username'] = $config['username']; } if ($config['password']) { $extra['password'] = $config['password']; } return $this->_manager = new \MongoDB\Driver\Manager($mongo, $extra); } public function getDB() { return $this->_db; } public function setDB($db) { $this->_db = $db; return $this->_db; } public function getBulk() { return new \MongoDB\Driver\BulkWrite; } public function getWriteConcern() { return new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); }
/** * 插入数据 * @param $db 数据库名 * @param $collection 集合名 * @param $document 数据 array格式 * @return */ public function insert($collection, $document) { $bulk = $this->getBulk(); if ($this->isAssoc($document)) { if (!($document['_id'] ?? false) || !($document['_id'] instanceof \MongoDB\BSON\ObjectID)) { $document['_id'] = new \MongoDB\BSON\ObjectID; } $bulk->insert($document); $this->lastInsertId = $document['_id']; } else { foreach ($document as $val) { if (!($document['_id'] ?? false) || !($document['_id'] instanceof \MongoDB\BSON\ObjectID)) { $val['_id'] = new \MongoDB\BSON\ObjectID; } $bulk->insert($val); $this->lastInsertId = $val['_id']; } } $res = $this->_manager->executeBulkWrite($this->_db . '.' . $collection, $bulk); if (empty($res->getWriteErrors())) { return true; } else { return false; } }
/** * 删除数据 * @param array $where * @param array $option * @param string $db * @param string $collection * @return mixed */ public function delete($collection, $where = array(), $option = array()) { $bulk = $this->getBulk(); $bulk->delete($where, $option); return $this->_manager->executeBulkWrite($this->_db . $collection, $bulk); }
/** * 更新数据 * @param array $where 类似where条件 * @param array $field 要更新的字段 * @param bool $upsert 如果不存在是否插入,默认为false不插入 * @param bool $multi 是否更新全量,默认为false * @param string $db 数据库 * @param string $collection 集合 * @return mixed */ public function update($collection, $where = array(), $field = array(), $upsert = false, $multi = false) { if (empty($where)) { return 'filter is null'; } $bulk = $this->getBulk(); $write_concern = $this->getWriteConcern(); if (isset($where['_id'])) { $where['_id'] = new \MongoDB\BSON\ObjectId($where['_id']); } $bulk->update($where, array('$set' => $field), array('multi' => $multi, 'upsert' => $upsert)); $res = $this->_manager->executeBulkWrite($this->_db . '.' . $collection, $bulk, $write_concern); if (empty($res->getWriteErrors())) { return true; } else { return false; } }
public function execute($collection, $where, $operator, $upsert = false, $multi = false) { $bulk = $this->getBulk(); $write_concern = $this->getWriteConcern(); if (isset($where['_id'])) { $where['_id'] = new \MongoDB\BSON\ObjectId($where['_id']); } $bulk->update($where, $operator, array('multi' => $multi, 'upsert' => $upsert)); $res = $this->_manager->executeBulkWrite($this->_db . '.' . $collection, $bulk, $write_concern); if (empty($res->getWriteErrors())) { return true; } else { return $res->getWriteErrors(); } }
public function selectById($collection, $id, $options = array()) { $filter = ['_id' => new \MongoDB\BSON\ObjectID($id)]; $res = $this->query($collection, $filter, $options); foreach ($res as $item) { $data = $this->objToArray($item); } return $data; }
public function query($collection, $filter, $options) { if ($filter['_id'] ?? false) { if (is_array($filter['_id'])) { $param = []; foreach ($filter['_id'] as $id) { $param[] = new \MongoDB\BSON\ObjectID($id); } $filter['_id'] = [ '$in' => $param, ]; } else { $filter['_id'] = new \MongoDB\BSON\ObjectID($filter['_id']); } } $query = new \MongoDB\Driver\Query($filter, $options); $res = $this->_manager->executeQuery($this->_db . '.' . $collection, $query); $data = array(); foreach ($res as $item) { $tmp = json_decode(json_encode($item), true); if (isset($tmp['_id'])) { $tmp['_id'] = $tmp['_id']['$oid']; } $data[] = $tmp; } return $data; }
/** * 执行MongoDB命令 * @param array $param * @return \MongoDB\Driver\Cursor */ public function command($param) { $cmd = new \MongoDB\Driver\Command($param); return $this->_manager->executeCommand($this->_db, $cmd); }
private function isAssoc($arr) { return array_keys($arr) !== range(0, count($arr) - 1); }
/** * 按条件计算个数 * * @param string $collName 集合名 * @param array $where 条件 * @return int */ public function count($collName, array $where) { $result = 0; $cmd = [ 'count' => $collName, 'query' => $where, ]; $arr = $this->command($cmd)->toArray(); if (!empty($arr)) { $result = $arr[0]->n; } return $result; } public function objToArray($data) { return json_decode(json_encode($data), true); } }
|