您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16088|回复: 0
打印 上一主题 下一主题

[php学习资料] 升级PHP7操作MongoDB

[复制链接]
跳转到指定楼层
楼主
发表于 2019-3-19 14:24:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用 PHP+MongoDB 的用户很多,因为 MongoDB 对非结构化数据的存储很方便。在 PHP5 及以前,官方提供了两个扩展,Mongo 和 MongoDB,其中 Mongo 是对以 MongoClient 等几个核心类为基础的类群进行操作,封装得很方便,所以基本上都会选择 Mongo 扩展。
详情请见官方手册:http://php.net/manual/zh/book...
但是随着 PHP5 升级到 PHP7,官方不再支持 Mongo 扩展,只支持 MongoDB,而 PHP7 的性能提升巨大,让人无法割舍,所以怎么把 Mongo 替换成 MongoDB 成为了一个亟待解决的问题。MongoDB 引入了命名空间,但是功能封装非常差,如果非要用原生的扩展,几乎意味着写原生的 Mongo 语句。这种想法很违背 ORM 简化 DB IO 操作带来的语法问题而专注逻辑优化的思路。
详情也可参见官方手册:http://php.net/manual/zh/set....
在这种情况之下,MongoDB 官方忍不住了,为了方便使用,增加市场占有率,推出了基于MongoDB 扩展的库:https://github.com/mongodb/mo...
该库的详细文档见:https://docs.mongodb.com/php-...
MongoDB 驱动
如果使用原驱动的话,大致语法如下:
  1. <?php, C; \) F. v% C1 _- l
  2. , c8 {: C  e& Z' I; Y6 j6 q
  3. use MongoDB\Driver\Manager;% T+ l) F/ H6 |6 y9 k& K
  4. use MongoDB\Driver\BulkWrite;8 @5 H$ ?+ Q, }9 M) `
  5. use MongoDB\Driver\WriteConcern;/ _6 g" Q3 C; `0 I
  6. use MongoDB\Driver\Query;
    ; S9 V$ R- l3 S. U; a
  7. use MongoDB\Driver\Command;/ l  `7 T% H) Z# m) N
  8.   p/ o0 ~( t6 ?5 z5 ?) ?/ P/ M+ x. E$ S
  9. class MongoDb {
    5 x$ G& ~3 X6 w9 f+ O. t

  10. 4 |" j) K5 k9 T6 p/ h
  11.     protected $mongodb;" ^5 n; p6 J9 u) j. T" h" r! H
  12.     protected $database;
    ) C  L1 E( `5 }' `9 ~
  13.     protected $collection;5 k: f+ |/ J! H( d1 Q
  14.     protected $bulk;! g1 g/ \3 x' l0 Y; Q7 ~' J
  15.     protected $writeConcern;
    1 b6 u- f; B* a6 u$ z
  16.     protected $defaultConfig
    : K( a$ A$ T; ^6 [. i( `
  17.         = [, M/ m9 ]% O  `5 K3 t
  18.             'hostname' => 'localhost',, R# ?; `" K) w
  19.             'port' => '27017',) ]* H  T8 |& k. j; h
  20.             'username' => '',( z1 e+ D  `' {
  21.             'password' => '',
    2 L- q) F5 @7 |' q0 K
  22.             'database' => 'test'' \- i9 w5 L% N5 L3 a/ u
  23.         ];5 I2 K8 N0 x& F' S+ g( [8 f
  24. 3 j# r  \; p6 y. s2 _: B2 n
  25.     public function __construct($config) {4 o3 d, m/ E+ S9 N/ o
  26.         $config = array_merge($this->defaultConfig, $config);% h$ k% t! [' t: B9 U6 p
  27.         $mongoServer = "mongodb://";
    & D6 w! C7 O# L3 Q8 W7 h" \) H
  28.         if ($config['username']) {
    ( J3 X9 Y* `8 C1 w
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    . ~$ w) s9 p: M* Q2 r3 G( b( I, }
  30.         }
    $ o$ l/ {  w" H9 j
  31.         $mongoServer .= $config['hostname'];
    ( N2 N3 B- e0 U5 O2 l
  32.         if ($config['port']) {
    3 ?; u2 d, L8 `" A
  33.             $mongoServer .= ':' . $config['port'];, }2 D6 E" w1 A1 k' g" o" v" d: M% C
  34.         }
    - e7 h1 c' m4 g8 j2 N
  35.         $mongoServer .= '/' . $config['database'];
    ( a$ T  o2 \0 H" t2 i+ Y1 O/ M
  36. - M/ P- _% O. U" ^2 e4 d
  37.         $this->mongodb = new Manager($mongoServer);
    ; l6 C- ~% p1 v
  38.         $this->database = $config['database'];
    5 g4 D5 m* H4 h+ _
  39.         $this->collection = $config['collection'];
    ; M* k& z3 f0 ^
  40.         $this->bulk = new BulkWrite();
    5 \! A0 f  q3 ]1 m
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);8 |- X) z& y3 Y2 o: ^
  42.     }
    : {( ^5 k$ p3 a+ R) c

  43. 4 v) s( K- X2 m  i& n. L
  44.     public function query($where = [], $option = []) {
    8 e; V% ^  B7 y9 |
  45.         $query = new Query($where, $option);0 T* r) n: r1 N) [. Z: M+ @0 Q
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    ) t6 }7 j0 r1 K7 e5 `) _+ v

  47. 7 c% z0 d" V% T; O% p. h8 ]
  48.         return json_encode($result);
    ' l+ P* ]3 E5 n0 T, t( @7 z
  49.     }7 g1 ^4 Z. f* V
  50. 5 E5 x) N5 n: @7 L& `* O% ]/ d
  51.     public function count($where = []) {
    9 N- v+ W3 h  g
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);1 x* |  r% V- d6 M# ]! o+ `. M! l
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    : g; u" f/ J: a0 F
  54.         $res = $result->toArray();
    # l' {- w+ [- i# e6 a, U
  55.         $count = 0;$ a- e$ x+ u: h1 c/ g5 Z6 `
  56.         if ($res) {, F& L% ^8 N% l; T  X7 u
  57.             $count = $res[0]->n;
    4 `' \: p9 I* X
  58.         }3 T! M$ d; M7 c; _- P! g$ z) F
  59. 2 @* k9 a# J; b/ E& ~
  60.         return $count;  d/ b; K( O* Y5 Y- L
  61.     }5 D6 r2 Z9 Y* x# R( u, ^

  62. : N. B' A4 D( i9 j" g8 u; D
  63.     public function update($where = [], $update = [], $upsert = false) {  D& L6 v0 g! H6 k& x9 g( X
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);  T% i' T- r4 x: _0 P
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    & {' ^$ W6 [5 f

  66.   ^1 o9 {/ }0 a7 y; v
  67.         return $result->getModifiedCount();
    ( v  _: B+ j0 Q0 |1 K
  68.     }
    5 Z. U# Z& t: M) E  D
  69. 0 t+ D. x0 M1 l  U0 Z+ R+ o5 c& m& e& r
  70.     public function insert($data = []) {& d; u" S' L7 t1 `5 Q
  71.         $this->bulk->insert($data);- Q$ N/ [) M+ w4 F# y+ y% y
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
      ]3 R, }6 O9 [" p. ]
  73. 9 @% Q8 y0 y6 b# g* \
  74.         return $result->getInsertedCount();
    0 l' K7 M; U! G
  75.     }
    $ @6 `0 k2 c- P+ N$ q* l8 |

  76. 0 o% R* s: U- O$ g- i
  77.     public function delete($where = [], $limit = 1) {
    ' l" X2 m6 Q$ t3 k
  78.         $this->bulk->delete($where, ['limit' => $limit]);$ T/ h8 G7 x( s% ~' |- q9 k% `- Q
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    + e& @2 g( R8 T) J" N0 p

  80. # `9 o  e, y9 v
  81.         return $result->getDeletedCount();
    6 G, C; B, m  T
  82.     }
    8 N% _# P! J; E9 I  T
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • # z! s5 H' D8 n
  1. new MongoClient();
复制代码
  • 1 B4 O$ c3 b7 F
  1. new MongoDB\Client();
复制代码
2.新增

  • 0 [+ C, j. S6 M  b( j+ H- L5 ^
  1. $collention->insert($array, $options);
复制代码

  • # @! z2 P- ~* R" m9 P* f
  1. $resultOne = $collention->insertOne($array, $options);//单
      g$ U2 k* U- x/ O
  2. $lastId = $resultOne->getInsertedId();
    $ u% o+ g- q/ J5 J4 S+ s
  3. $resultMany = $collention->insertMany($array, $options);//多
    1 j) G6 v; Z# z2 I
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • ) P# ^: n. @7 q0 E# A( Z
  1. $collention->update($condition, [
    ) b+ f. X, S% U4 m; d
  2.     '$set' => $values9 x7 B4 [% J" Q9 Y/ h2 l2 z( z
  3. ,[
    3 ?! z7 m" P' B6 U/ a
  4.     'multiple' => true//多条,单条false5 z8 L# n& N4 i! w7 Z' {) u0 E
  5. ]);
复制代码
  • % X* J8 S. n1 I, d! v: d& `" V
  1. $collection->updateOne(
    . k$ f0 v2 M0 ~) r  c
  2.     ['state' => 'ny'],
      M7 J, W2 Y7 W* T# ~
  3.     ['$set' => ['country' => 'us']]
    * |/ l7 q6 f1 g! u+ m5 [$ O& t
  4. );
    8 m. [2 i# P9 H0 ~
  5. $updateResult = $collection->updateMany(
      }  v/ L& q' }0 `9 v6 D7 L' H* I
  6.     ['state' => 'ny'],; Z) e. Q- M+ i9 z1 x5 V/ y
  7.     ['$set' => ['country' => 'us']]
    3 l+ \, `. e* D3 x; a
  8. );
    5 |% V! g4 E5 R6 M/ C& O/ v
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • 2 s& ]) u- ?& @/ ]6 }
  1. $cursor = $collection->find($condition, [. w9 Z, U" k! @# ^* n/ L
  2.     'name' => true//指定字段+ x4 H6 E' D6 z% I; w
  3. ]);7 n+ n+ A& y+ \* l. h. ]& r
  4. $cursor->skip(5);
    $ b5 c! ~( D/ {+ m1 }
  5. $cursor->limit(5);
    7 G) \) E# R2 W) W. n1 u
  6. $cursor->sort([
    8 B9 H' V" u# L! v6 V
  7.     'time' => -1
    " }# s" [! s5 r5 v! N. L0 h
  8. ]);
复制代码
  • 0 Z5 `+ w' Q& S: j6 V8 R
  1. $cursor = $collection->find($condition, [
    $ y7 {( l+ M4 H
  2.     'skip' => 5,% l/ T# z+ E$ ]0 Z7 [8 g0 N$ Z
  3.     'limit' => 5,
    ' h( b% L* }0 n+ v( {
  4.     'sort' => [
    - k; W* a+ ?/ \8 H
  5.         'time' => -1
    ! r; R, t( d) |
  6.     ],//排序
    ( ~2 i) J3 {! o1 @& M
  7.     'projection' => [
    2 x6 ~" s- x  J/ p  |4 i
  8.         'name' => 1//指定字段
    7 J( I# |0 e1 Q$ G4 K
  9.     ]1 k( T  V$ T4 U2 B6 ^, k
  10. ]);
复制代码
5.删除
  • 9 |+ y9 ^# Q7 s9 J* X
  1. $collention->remove($condition, [
    1 Z( y& |& z' U* j
  2.     'justOne' => false//删单条  P" P; v6 n) B9 A) F& ]/ m8 I6 F
  3. ]);% A( i) j) ^8 }" v/ ?4 `
  4. $collention->remove([]);//删所有
复制代码
  • 9 l4 R5 z- ?8 I1 S- s1 p2 X7 ?
  1. $result = $collention->deleteOne($condition, $options);( h$ }% I' v+ N. H6 Z
  2. $collention->deleteMany($condition, $options);  F# U" W5 p+ A9 j; U# u. s1 G

  3. % ^: ~9 S3 J! [5 e' {
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([/ q, B# n- Z7 ]
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键* n, g- T% [, S' q: K7 o: @
  3. ], [9 s& l' a7 a3 o, E
  4.     '$inc' => ['id' => 1]//自增
    7 v" R$ S$ \" |# p/ A* ?' h, }$ I
  5. ], [% U8 d# ?- ^" s3 B( d
  6.     '_id' => 05 C3 h4 X0 b8 C8 Q3 a% ]/ {! W& d
  7. ], [" S/ |; H$ P; u# \( _
  8.     'new' => 1//返回修改后的结果,默认是修改前的+ v0 R$ B6 R2 ?4 w
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([( n4 P& u7 p5 V& `  w3 p
  2.     '_id' => $tableName
    ; f5 o. i; m# ^, O8 w. g
  3. ], [- S7 X  D2 k8 d+ s( g
  4.     '$inc' => ['id' => 1]
    - \) ~- M1 x* E( _* X! T
  5. ], [
    $ o" |( X: i0 ?( q  j8 Y  U
  6.     'projection' => ['id' => 1],1 s1 C; C' K6 J
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    2 a" L0 H9 s3 D. w. p
  8. ]);
复制代码

2 q$ p9 c- L) N
/ P* z1 n2 D. l% M) _, @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 18:44 , Processed in 0.054787 second(s), 19 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!