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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16364|回复: 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
    4 u' [( n* [$ h2 ~% u

  2. " D" @! f1 d: _1 ?
  3. use MongoDB\Driver\Manager;7 r7 J0 [& E. B: a7 E
  4. use MongoDB\Driver\BulkWrite;
    ! `+ s2 |: n; r# e. W
  5. use MongoDB\Driver\WriteConcern;( p; E+ |  D& B& i* h
  6. use MongoDB\Driver\Query;
    ) e) W3 m4 A8 g! L& ^  W- j
  7. use MongoDB\Driver\Command;  i, A. V; E8 e

  8. # @. z0 i) d; I8 N: Y/ k
  9. class MongoDb {. o8 j& I+ N% `; d* v
  10. 4 u! Y9 k4 f1 o# `$ N
  11.     protected $mongodb;
    # W) {2 V3 l: Z
  12.     protected $database;
    # I9 E4 {, P% W  o* F
  13.     protected $collection;. G+ h# |& f8 _* @" W' }& ]! x5 T
  14.     protected $bulk;
    & ?  T( a* G0 A/ X; n  r6 ]% C
  15.     protected $writeConcern;
    * x- W% W% W' L5 x8 n
  16.     protected $defaultConfig
    8 `4 v; ]: r2 s# V4 |, v/ @
  17.         = [
    . J# \- V/ g5 ~( y4 i
  18.             'hostname' => 'localhost',
    0 _5 R7 y8 ~& h" t
  19.             'port' => '27017',
    # B- i9 r* `7 T+ E: f6 v7 l
  20.             'username' => '',& J& q/ `  }+ S2 Q) x+ m
  21.             'password' => '',- c! c3 Y4 v- h* V. Z1 f! o
  22.             'database' => 'test'
    8 H: J; O7 v# O2 z* ]+ N$ X  {
  23.         ];
    ) D% Z" S' G" o: L0 _  U
  24. $ r% }8 ]/ j5 s
  25.     public function __construct($config) {. V( R, r% m+ X, R; ]
  26.         $config = array_merge($this->defaultConfig, $config);
    3 X, O) a5 E0 U' j. @! X
  27.         $mongoServer = "mongodb://";1 z! v8 {! ]2 w  ?( w! C8 F, m
  28.         if ($config['username']) {
    + l% j% k% i" d# j. F: n. G9 [, K
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    , w, F' j  n5 W" f+ C. o, e6 u
  30.         }
    % Q! @. W5 R) p, C: }
  31.         $mongoServer .= $config['hostname'];4 h% |% c$ G0 I
  32.         if ($config['port']) {
    9 R8 i- U8 ~$ s  p
  33.             $mongoServer .= ':' . $config['port'];  Y" Y/ P$ F( }7 h* V
  34.         }
    2 B5 \( F  @$ y1 J
  35.         $mongoServer .= '/' . $config['database'];* c5 u& N8 \) B1 |  |

  36. 5 S( B9 s6 ]+ U) F4 g1 [" ]8 ~  q
  37.         $this->mongodb = new Manager($mongoServer);
    - }8 Z/ l1 h7 c  N4 F, i9 l
  38.         $this->database = $config['database'];
    ; U. D/ p9 I6 N
  39.         $this->collection = $config['collection'];
    - J- m% i* Y) a! P, s
  40.         $this->bulk = new BulkWrite();
    1 J5 w4 J3 t. e% L* Z. G
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    / j4 [6 a* k+ V& P2 B
  42.     }) K+ v: o; u, i$ n( s2 h* C/ Y- ~
  43. % M4 V" q& |" |
  44.     public function query($where = [], $option = []) {
      B* R! `! n: }
  45.         $query = new Query($where, $option);4 u$ ~3 C7 [5 q. i
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);# W. A, Q5 s; b) f: T$ t( Z

  47. / l& ?- g3 G9 M* B) `$ V
  48.         return json_encode($result);* a4 t( P' J) y' d6 f
  49.     }7 ^/ ]% u7 C; O" R# @$ c5 d
  50. # g$ R0 a' V3 G5 w& [' r+ k5 _
  51.     public function count($where = []) {
    3 R- y3 a0 f  b. Z
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);& o% @+ b( Z& e; S4 a. n
  53.         $result = $this->mongodb->executeCommand($this->database, $command);7 s' f. M& e! U/ a$ m+ N* q
  54.         $res = $result->toArray();3 y1 ]! U3 f5 v
  55.         $count = 0;' g% u" c( t  [6 P
  56.         if ($res) {
    ( x3 [9 @. V( B# y
  57.             $count = $res[0]->n;' a/ F: O$ h* |& U3 A! y. B, j+ k
  58.         }
    ' l; I8 Y! N, j5 y" S$ t

  59. ( x# G" G& i. z: W
  60.         return $count;4 O6 Y6 {% T1 [# h0 {- B
  61.     }9 h6 O7 b5 p7 V0 Y4 N4 y6 T( X
  62. 9 @$ ~$ e0 P5 W& P
  63.     public function update($where = [], $update = [], $upsert = false) {4 e0 @; b7 N5 S" e, W3 |. ~, M
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    # s6 J3 S& e' P' B
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    0 O8 p, n# f* o/ j/ ^  z, k
  66. ; Y* N# m, H& O0 ^. N7 {6 a
  67.         return $result->getModifiedCount();
    3 m0 p; H4 |3 T# t$ I( N! L7 h
  68.     }
    4 Q; {! t( d, ]

  69. 2 o. D( r1 M' r% J- g3 N0 s
  70.     public function insert($data = []) {' R% A4 O& G9 X9 v" N: [/ H
  71.         $this->bulk->insert($data);* x0 Q$ ?& R& A6 w  ^* d+ G
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);4 I5 ^9 Z1 Q( v$ Z  Y

  73. 6 v8 O- e" x2 ]& H
  74.         return $result->getInsertedCount();
    + z2 k# [6 g3 `+ y
  75.     }/ P/ S5 z2 n. |; b' S
  76. ' B) A2 }! D' D) |+ Q; i: A" |# O
  77.     public function delete($where = [], $limit = 1) {
    . W- H% A0 Q, J8 q# J3 u3 h
  78.         $this->bulk->delete($where, ['limit' => $limit]);! |1 Y( D' j0 ]1 G$ C" T- u4 k
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);. @+ `0 ]( ~6 m  _% v2 P

  80. ! c0 f' }* }( Q/ Z; ^# D
  81.         return $result->getDeletedCount();
    8 o, U5 `# u6 Z  E1 A, p
  82.     }
    . Y3 }* I9 j4 p2 [5 {, Y' T
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接

  • 7 Z$ [( E" l, c4 G2 W. m8 Z$ q4 e
  1. new MongoClient();
复制代码
  • ' c* b) l' w( e+ s1 S  K) A
  1. new MongoDB\Client();
复制代码
2.新增
  • ( |2 g$ t8 e" `9 P# N6 X% v
  1. $collention->insert($array, $options);
复制代码
  • 1 h1 y# ^; V- c8 n9 N* x, Z# q
  1. $resultOne = $collention->insertOne($array, $options);//单, H0 @4 u: e  N# ^4 E7 Y
  2. $lastId = $resultOne->getInsertedId();$ x& O7 Z4 a0 R
  3. $resultMany = $collention->insertMany($array, $options);//多" w" F8 g9 d5 [9 Q7 T2 k* n1 ]
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 3 F9 w; c/ b0 h5 C6 V
  1. $collention->update($condition, [
    ) k1 a- t1 {2 |$ k  u" G& W
  2.     '$set' => $values
    8 K0 |+ ]6 s2 q  w6 Y, `) f: _4 D
  3. ,[+ k! U; m5 X1 T; `2 ]+ F
  4.     'multiple' => true//多条,单条false
    4 I8 Q+ G8 Q- Q/ n3 L8 q3 B4 B
  5. ]);
复制代码
  • - w* G- ]0 I1 L4 P
  1. $collection->updateOne(
    ( ~4 Q; m! G( c- H; _
  2.     ['state' => 'ny'],' n5 J2 i/ Q6 G
  3.     ['$set' => ['country' => 'us']]# n, s. X6 g& {* ?- F8 B0 @2 L0 k
  4. );- a! p' i- k0 H3 c- o2 J1 S: e
  5. $updateResult = $collection->updateMany(4 b6 g5 N3 b1 r9 J7 V
  6.     ['state' => 'ny'],
    / g( L7 F$ y% o5 w) o
  7.     ['$set' => ['country' => 'us']]
    . D, c6 y! Y) b5 [9 ~* o
  8. );6 G& Z& F) B. W5 m+ ~  r
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • 3 S! a+ d4 f. o& X! N' P( q8 v$ _. `
  1. $cursor = $collection->find($condition, [
    ( l+ `; h/ K0 C: I2 m! n
  2.     'name' => true//指定字段
    ' }  g8 s  x: w1 [# c
  3. ]);2 p; \# N9 Q# ^/ {( H2 p
  4. $cursor->skip(5);5 d6 o) }  X& }* ^
  5. $cursor->limit(5);) N2 V9 O! j( L5 T
  6. $cursor->sort([2 D$ @$ @5 Q; J; q; @- n& S9 P6 a$ f5 [/ c# ^
  7.     'time' => -15 Y: |( `5 Y: s4 @/ r3 u! J$ ?7 w
  8. ]);
复制代码
  • ( O. S& I4 f  g8 e
  1. $cursor = $collection->find($condition, [
    2 _5 K% p1 d+ L* O+ T
  2.     'skip' => 5,
    ' ]+ x2 ^! R3 G! p$ z  T, @
  3.     'limit' => 5,: `2 w' d) ~4 g; a+ q
  4.     'sort' => [
    " v+ o' v; |6 p! f8 {0 n
  5.         'time' => -1
    . A  S6 A9 X2 M) G$ s
  6.     ],//排序
    ! d5 m! U! J+ W( H4 i+ R  |
  7.     'projection' => [
    - T1 n  t% f* U' y
  8.         'name' => 1//指定字段. {# l6 U" c) [' ?
  9.     ]' B4 W2 N" ~' ]( q( y* p
  10. ]);
复制代码
5.删除
  • & z. z8 k1 J1 z% ~9 X- V
  1. $collention->remove($condition, [
    , r- s2 E- K/ i
  2.     'justOne' => false//删单条3 C: I. _- L" k2 O! j( d
  3. ]);9 I6 I6 y" @! w9 Q. `
  4. $collention->remove([]);//删所有
复制代码

  • " M  h) h- @& g# B" A' ]
  1. $result = $collention->deleteOne($condition, $options);" R* y9 ^+ w2 ^2 [4 M
  2. $collention->deleteMany($condition, $options);) [! ?. [- q: W( W0 g5 n% c
  3. + o+ N' Z2 y, f. u# \! N
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([1 X+ h4 F9 A, _" a% [: U
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    " E1 v) T; ]5 w
  3. ], [
    # f. |; D: N( J, Z6 `) b) H
  4.     '$inc' => ['id' => 1]//自增5 e2 H1 E# N3 s/ R. i! F. y% X) y
  5. ], [1 S) b/ Q- b2 l
  6.     '_id' => 07 Q# q6 m" {& X* O0 F8 z5 P
  7. ], [
    0 H* N1 b; a9 R0 s8 r1 X& F
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    # h/ {! ]3 {, [, ]
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([: ~" p9 c3 t+ t2 x+ I8 ?) B
  2.     '_id' => $tableName7 I. ^: @9 g5 C/ a% S
  3. ], [
    ' M4 F/ X( D) M) G& `8 p9 i* L
  4.     '$inc' => ['id' => 1]  T! z4 _0 q2 K; o% l
  5. ], [
      ]/ w3 R2 f  t6 y9 w+ P8 u/ a
  6.     'projection' => ['id' => 1],
    , Q6 w! d1 O3 R+ Q, j, G3 B, o% M
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    4 \. S! v+ H9 x# e! `" u2 c
  8. ]);
复制代码

% ^4 e. R* r2 E" r' d0 f+ r4 b
. {' }2 E; b/ S9 ~( D
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 14:34 , Processed in 0.056680 second(s), 20 queries .

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