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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 15969|回复: 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
    1 [7 ~+ Y1 j% ]& O! ^! p. ~* G$ T2 d" h
  2. " ~$ G; u' m. e) N8 w( ^
  3. use MongoDB\Driver\Manager;
    4 H( \$ O4 h4 s1 {7 T* M
  4. use MongoDB\Driver\BulkWrite;
    6 w. z1 a: I% t& I1 p9 k& q
  5. use MongoDB\Driver\WriteConcern;0 E$ I! C; }. J, R5 p2 o/ L6 S
  6. use MongoDB\Driver\Query;
    , G9 D: M0 X1 J0 G9 q" n5 b- x
  7. use MongoDB\Driver\Command;
    9 ~4 C8 m( U/ z

  8. ' C* D9 N3 Y/ S& P; g/ }; k
  9. class MongoDb {4 _- S; B" M% V$ u) T! j0 k
  10. 8 ^. r7 h" ~' V
  11.     protected $mongodb;
    - F6 n- ?! D! p. ?) ?0 J& m0 s1 y
  12.     protected $database;+ i1 q+ x9 O2 P) u: j$ e! f
  13.     protected $collection;
    ; H( {+ c6 |+ |$ q/ y9 q/ I
  14.     protected $bulk;
    - Q' f- D7 |- G. o4 ], F
  15.     protected $writeConcern;
    0 n9 L; W9 S$ |8 s5 X
  16.     protected $defaultConfig) o& e6 H5 N. P
  17.         = [
    " t: W5 f" [4 T6 ^
  18.             'hostname' => 'localhost',
      q5 k# j- s: Q$ A1 W
  19.             'port' => '27017',
    ' W- m, g+ n) I: p
  20.             'username' => '',
    9 o1 S- G# ~4 I4 _8 r
  21.             'password' => '',
    ; S* ]" V4 p5 M0 P7 A( [( I0 X0 P
  22.             'database' => 'test'
    ; M& q! e0 {& z6 g0 ~
  23.         ];
    ( g% F3 n. M( H' Q

  24. & l5 C. l: _9 m
  25.     public function __construct($config) {( j. q1 ?6 ]" b, Z" t% Q7 C
  26.         $config = array_merge($this->defaultConfig, $config);
    " Y# @# ]- v6 x0 B3 G
  27.         $mongoServer = "mongodb://";
    * e  T0 g' ]1 }" J0 G/ g1 N% |
  28.         if ($config['username']) {
    / a/ Y0 M' Z" n& e- I2 A
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    / @, ~- K/ M' \& X1 J
  30.         }
    6 }& C3 |/ b5 ?
  31.         $mongoServer .= $config['hostname'];0 w2 z9 ?$ T; v* m* r7 A! K3 G# W
  32.         if ($config['port']) {! [/ M- \9 ?% V. s* i
  33.             $mongoServer .= ':' . $config['port'];
    ; _, \( e. r. a4 a3 @
  34.         }
    & R% {; W8 P$ L: p  a) B! M/ E
  35.         $mongoServer .= '/' . $config['database'];
    / x' \; ^  y/ s2 c- F5 ^; h: U. l/ ~  y
  36. " \% W% {2 a* @
  37.         $this->mongodb = new Manager($mongoServer);+ v, e% r3 ?7 z8 a9 W
  38.         $this->database = $config['database'];
    . B6 E2 ^/ [* d+ u! [
  39.         $this->collection = $config['collection'];# ^: m' b0 u3 z, G
  40.         $this->bulk = new BulkWrite();
    % l7 z+ f0 D! m. N& Z1 e! c
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    ! g; d" v  b6 O  C0 o
  42.     }7 T8 V, P' d! [4 Q" A, x$ V' j6 A; A

  43. & ]; Y; c" B3 G7 \
  44.     public function query($where = [], $option = []) {
    " ?7 {0 n- G" F- z0 G% W! D, Z. Y) ?
  45.         $query = new Query($where, $option);
    " b1 L  M1 l/ N% }' P
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    6 J( W. u8 m% ~+ H; B8 w

  47. 6 c& }5 E) X! A9 ^
  48.         return json_encode($result);  e  `0 g, t. h/ W
  49.     }
    2 w7 d+ j) E/ _
  50. + F- _9 B: ?- u3 I6 y. Y, u
  51.     public function count($where = []) {
    % K& B+ s0 ]/ e8 p
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    ( ?# m: i4 J" Q# C
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    & t/ x! e: a( I+ [
  54.         $res = $result->toArray();
    2 x7 r. U# W+ W
  55.         $count = 0;( V" e2 Y1 S6 U
  56.         if ($res) {' o) F* |+ X6 c' c0 Z+ B- Q
  57.             $count = $res[0]->n;
    * C0 D+ G5 }! a; f1 X; Z6 s
  58.         }
    - j. c& ]# o5 e: Y
  59. : |' C( u, M/ G/ r8 Y9 S' @, t
  60.         return $count;
    1 |& @: [$ E. X7 f% z9 r% u
  61.     }. {; P' D2 A3 W! b9 D3 c

  62. 0 G8 x1 H. J' c& P
  63.     public function update($where = [], $update = [], $upsert = false) {- y" [0 s5 e2 @0 f
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);, d' u2 c, q  v
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    % z- q: Y; W( a4 B% J

  66. ; i, f% s+ e9 \) m* Y+ _
  67.         return $result->getModifiedCount();
    ( [6 M. r( N- e+ x% E$ ~/ J; b
  68.     }
    3 U# A) E5 f+ m- \: \0 {" i0 x) M
  69. 2 T4 @) Z/ P1 r* f# e+ `
  70.     public function insert($data = []) {
    ! Q7 B9 g, w; q$ v* c7 B' K
  71.         $this->bulk->insert($data);0 O4 K2 L" ]/ _# r' J- A
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);! p& z3 @, M0 }6 l" N! l# v1 ~$ u
  73. 8 M6 [) a. |% ^+ C* j$ @
  74.         return $result->getInsertedCount();+ a  {6 q, D5 {7 w- |
  75.     }
    9 R2 \; @0 A  n6 s

  76. / M. O2 [8 X7 V4 |; L6 s4 b
  77.     public function delete($where = [], $limit = 1) {; `8 }1 q% Y) W3 Y6 W6 s7 Y
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    . u1 G9 U! b0 L  R4 O
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);0 Y5 x; l' M$ h) p+ G3 Y

  80. : a) r/ `9 t0 J1 F
  81.         return $result->getDeletedCount();. G% ]% S9 ~' {% f& f
  82.     }; U, H) p( M# f
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • 8 h3 X. z; u, ]% I. Q: @. \* l
  1. new MongoClient();
复制代码

  • & x  q+ i( \) a5 M6 S
  1. new MongoDB\Client();
复制代码
2.新增

  • $ Z, @+ ]$ A* s" ]% ~
  1. $collention->insert($array, $options);
复制代码

  • " w0 `- G/ w1 Q( Q' T. @) A+ h
  1. $resultOne = $collention->insertOne($array, $options);//单
    + u$ g" {# k" n! @( @, W$ y
  2. $lastId = $resultOne->getInsertedId();
    ! Z9 v2 K# z% U1 u0 |# m
  3. $resultMany = $collention->insertMany($array, $options);//多. l+ x) z! k" U' {, {# g4 n
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • . d" L: x* [# \( Y% y$ B2 I+ ?
  1. $collention->update($condition, [* `4 e9 ?  B- S4 ^/ w; O6 H0 k8 w
  2.     '$set' => $values
    " k& E6 x- p/ X6 H4 @5 i2 D
  3. ,[
    ) K" P( g- q) f2 C
  4.     'multiple' => true//多条,单条false0 W+ N3 l( a7 e
  5. ]);
复制代码
  • ! @; G  n- ^9 `7 P7 j; i& @# J
  1. $collection->updateOne() X( g, N0 s: Q
  2.     ['state' => 'ny'],+ }9 C+ J1 s- W8 l% V, ~" g8 `2 \
  3.     ['$set' => ['country' => 'us']]
    9 l' b5 [3 n$ j0 Z& Q
  4. );
    / Y( H7 a, ^) S% T7 J' t
  5. $updateResult = $collection->updateMany(; H8 l8 Y+ J8 K6 M
  6.     ['state' => 'ny'],0 x  T6 i/ u0 d6 t$ T
  7.     ['$set' => ['country' => 'us']]7 L. v5 {) l! H; @
  8. );" L% j, }  U5 h0 m. `
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • ! C, |: R7 w2 Q7 G. J$ Q
  1. $cursor = $collection->find($condition, [5 ?9 n, T  ?& F4 G( e4 @2 w
  2.     'name' => true//指定字段
      H, a& p' s) P% K3 }4 y
  3. ]);0 [- ~) ]' q5 A2 m* m3 q6 s
  4. $cursor->skip(5);# O6 I; p* O* f8 M9 L# g
  5. $cursor->limit(5);6 J( Q! w& C6 `# s! T
  6. $cursor->sort([. u: m6 d+ o& _
  7.     'time' => -1
    $ K& W  l, ^) {2 w+ U! k
  8. ]);
复制代码
  • 4 R! S! h8 U: L# o2 O; H
  1. $cursor = $collection->find($condition, [
    : i7 N) ~& K! c+ r1 Q6 B
  2.     'skip' => 5,
    ; P* Q: q, n! W2 J# c' e: G
  3.     'limit' => 5,! P; C9 T. o) I- F; u0 c3 I
  4.     'sort' => [, L, l& v5 x6 r( J, u7 Y
  5.         'time' => -1
    # ~6 C& Z6 s/ s7 S
  6.     ],//排序, z1 z' h5 g& v, |9 U8 f4 O
  7.     'projection' => [
    $ `" }0 [: m1 S. P. \6 g, f8 O
  8.         'name' => 1//指定字段6 n, T5 x2 |; ^+ u5 s/ E9 L
  9.     ]
    & A* Y4 ~8 ^0 s# \
  10. ]);
复制代码
5.删除
  • % e0 z; j2 |% Y* W
  1. $collention->remove($condition, [  \3 b( x) ]6 `) M# |
  2.     'justOne' => false//删单条
    : M6 k! E, [' v( D. v# I+ N( c: k
  3. ]);- b* T3 c. N! ^3 X0 a7 }, q9 M
  4. $collention->remove([]);//删所有
复制代码

  • ) K  Z; s+ c. h0 Z
  1. $result = $collention->deleteOne($condition, $options);( Y$ `9 }# ]  ^  J, i7 X
  2. $collention->deleteMany($condition, $options);1 Q  I+ i3 X* x! G8 e% A
  3. 7 Q; O1 S1 g, H1 T* R5 c2 \
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([1 H& s+ V( u; A6 T+ i$ p
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    , N: I1 k  c# O% X% A  B- \
  3. ], [
    1 Y. B. q6 f' \& L( A( s* I
  4.     '$inc' => ['id' => 1]//自增
    9 d( A# J( c+ J' I
  5. ], [
    & ~( U# [& ]7 {5 x( k
  6.     '_id' => 0
    8 P* A& _! u; g! R& `" C
  7. ], [) T  K; n- n' f) e3 r8 y8 M
  8.     'new' => 1//返回修改后的结果,默认是修改前的( d4 F) @( R: A) b) y' H
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([- Q6 i( Z1 q' U4 ^7 c# J' c- w
  2.     '_id' => $tableName# r" N1 x& ^9 x" s
  3. ], [6 V# {& |# g) O2 F1 c
  4.     '$inc' => ['id' => 1]
    : c1 ^) j& o' Y9 ]8 ^2 ]
  5. ], [7 I8 r6 Y8 [1 l8 I
  6.     'projection' => ['id' => 1],
    % M+ a% \: h2 \7 }3 j7 p" }
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    ! c3 ?8 H  m* `( \
  8. ]);
复制代码
! ?. ~; Y* `" {

$ k& q, o2 n/ \8 N$ T
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 19:01 , Processed in 0.068075 second(s), 29 queries .

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