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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16360|回复: 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. <?php9 V, U, \4 Q0 w* K

  2. . j8 @9 P$ F6 `, R9 ~0 x
  3. use MongoDB\Driver\Manager;3 ?3 {2 U# p, X/ G1 [( i# D
  4. use MongoDB\Driver\BulkWrite;( A6 i! N: R4 c! M  X/ g
  5. use MongoDB\Driver\WriteConcern;
    ( F# d9 `  B; m1 {& p+ l
  6. use MongoDB\Driver\Query;
    " I9 ]7 ^/ ^6 D) s$ a
  7. use MongoDB\Driver\Command;
    7 M; R, H' W  [$ M/ G
  8.   ^; Z4 T4 p7 A, J4 Y
  9. class MongoDb {0 P; J/ t# u$ R7 x

  10. - p: `' J, _' h+ F$ V
  11.     protected $mongodb;9 Q! \1 J1 H* J0 {: ~7 z
  12.     protected $database;
    $ J+ K  d9 K+ g6 q5 I; A& W, l
  13.     protected $collection;; D  J2 A: R# e( ]( S, z9 C
  14.     protected $bulk;
    " t2 X3 N, X) K1 p+ x
  15.     protected $writeConcern;) |; i( Z5 D2 G8 S
  16.     protected $defaultConfig
    ; C% G, a8 ], N( x' p4 _: i5 t
  17.         = [) s# `# A/ A" I* g1 S4 V
  18.             'hostname' => 'localhost'," V/ s1 Q! }5 r4 s+ v. i$ }
  19.             'port' => '27017',9 J3 y- q& e7 v! Q
  20.             'username' => '',
    . O7 V3 u5 {) L0 ^- W" I1 C7 }% N
  21.             'password' => '',8 D+ T1 ~+ \9 K, f) i$ C- a" l
  22.             'database' => 'test'
    3 B' j. j0 R3 d% C( ~- T
  23.         ];: o& G$ h, y; j, y/ K0 Z3 }' V
  24. 0 ^% t( u! ?# Q# S; {5 e
  25.     public function __construct($config) {
    ' Y. Z' m, T' a0 P8 ^) s: g7 j
  26.         $config = array_merge($this->defaultConfig, $config);
    ' I' U, d  f  h# _% d
  27.         $mongoServer = "mongodb://";; S/ m- |! C* \/ p* W# x
  28.         if ($config['username']) {
    2 Z# u6 H* e* u: A3 m: l
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';# `1 G9 i' k3 k; Q. m( Q
  30.         }
    % w, M$ j3 {. K" y  u
  31.         $mongoServer .= $config['hostname'];6 U- d7 N3 z# `7 E* s- ?9 L7 v
  32.         if ($config['port']) {' C/ B  r) t$ f5 G4 C
  33.             $mongoServer .= ':' . $config['port'];
    ' ^# \" }7 Y1 N1 T. V9 x
  34.         }
    0 l1 j7 F9 j' D8 J$ M3 x
  35.         $mongoServer .= '/' . $config['database'];; w. v# P6 G8 D& H/ o
  36. - P, h$ U" a! f# A! q% W
  37.         $this->mongodb = new Manager($mongoServer);
    4 n" s. x% q; V
  38.         $this->database = $config['database'];9 X  h9 H. x; X  V5 a
  39.         $this->collection = $config['collection'];" U" v, n, I) C. O5 [; y: L6 M
  40.         $this->bulk = new BulkWrite();7 \7 S- q: _9 ~4 }& w
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    * U" ^; J6 _( Y9 K  n
  42.     }8 n' @. W3 T" ^

  43. 0 e+ }, q# U6 P* k  \& s
  44.     public function query($where = [], $option = []) {
    7 o2 S- E5 f" [# F! w
  45.         $query = new Query($where, $option);9 N" m0 N( h# Y, j  N2 |" ]( t
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);, w& w4 T3 H/ d9 ]3 o8 U' `: ^0 r

  47. ' f0 n6 @( I+ L: \: ^# ~2 t4 o2 U
  48.         return json_encode($result);
    4 B0 h; ?; V( c/ ^
  49.     }# A# X/ ~8 u* D3 w

  50. 4 M: s' C/ w4 K2 i0 i
  51.     public function count($where = []) {3 X7 Z. w! Q; m" z
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    + `3 J' g* L% i8 D! O, y8 N0 v) c; x
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    3 v- \) L, J, e
  54.         $res = $result->toArray();
    + e! O( r" o$ z2 C, Q; R4 f
  55.         $count = 0;$ k9 a7 B3 R6 u9 U: _2 `
  56.         if ($res) {
    * q1 J+ V6 l" n% l
  57.             $count = $res[0]->n;
    7 ?8 ^3 |5 |$ j, P3 D
  58.         }2 V$ b/ |" f# {0 b. N6 N

  59. 4 M8 q1 e$ U1 _# F$ q* s5 N
  60.         return $count;) i7 w) B2 p" ~3 S6 k/ M2 b+ E
  61.     }6 V, f5 b4 t# f$ A' f. h& d

  62. 8 A2 d7 l' Z. y1 g) i3 }+ c% _' G
  63.     public function update($where = [], $update = [], $upsert = false) {4 s- _2 d8 S$ ?5 L2 f: s+ o
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    , z3 D# e: P. _, u6 `0 G
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    3 U& @, H! F  c6 {- b

  66. / [% W" ~  k- |5 }. C& p+ Q
  67.         return $result->getModifiedCount();
    " _' s  R1 \4 k* M# n; h) g
  68.     }& L7 H) Q( [  E
  69. % D: j2 t: w5 Z' l8 D4 G
  70.     public function insert($data = []) {
    " @- W9 a' s/ r+ X$ e* k5 P
  71.         $this->bulk->insert($data);
    8 [4 Y0 {: a$ }
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);0 a! N9 |3 g% F- j
  73. / R- O8 r/ @" B. e
  74.         return $result->getInsertedCount();
    5 T1 ], W8 q1 o2 J0 N4 B
  75.     }, H+ t2 Y( Z  Y1 Q7 ~

  76. : m. t- t: I9 b  [* v
  77.     public function delete($where = [], $limit = 1) {! P8 G) E% g* S; s0 j2 b
  78.         $this->bulk->delete($where, ['limit' => $limit]);2 H! ?! o$ {% z8 ~
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);6 Q6 ^$ x6 T; M% f

  80. 7 V  E% N6 o1 ]: H" d
  81.         return $result->getDeletedCount();
    : W4 z+ C" U9 {& x
  82.     }: X2 ?* F6 e5 j1 M' A% m/ K4 l
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • $ f6 M1 I$ X0 j
  1. new MongoClient();
复制代码

  • - Q$ t5 O* Y  m
  1. new MongoDB\Client();
复制代码
2.新增
  • . W% b/ L7 g8 u& }5 {
  1. $collention->insert($array, $options);
复制代码
  • 7 }0 Q9 X( I3 [  K# B$ B# J1 r
  1. $resultOne = $collention->insertOne($array, $options);//单0 L8 u5 c/ ~1 [6 B( c
  2. $lastId = $resultOne->getInsertedId();
    / b. q8 k- d! C
  3. $resultMany = $collention->insertMany($array, $options);//多. c* m; O" M, e3 o
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 5 w+ j5 c4 Q+ }- {/ |" m
  1. $collention->update($condition, [
    % E$ q3 S9 T: Q, N5 R3 K
  2.     '$set' => $values$ V& E9 W1 q0 g' j- h& Q
  3. ,[
    . F, i# h  y" d7 v# s1 s
  4.     'multiple' => true//多条,单条false; i+ j1 Q; S7 z+ _' m& s; I
  5. ]);
复制代码
  • - @' e' j: ?- U
  1. $collection->updateOne(
    : O$ S* r4 X8 }  Y. c6 _
  2.     ['state' => 'ny'],
    / T6 x, k3 f4 s) v$ z  A
  3.     ['$set' => ['country' => 'us']]# N! t" h  ^- H: _& C
  4. );
    $ a+ ]6 B( h) ^. w) ~
  5. $updateResult = $collection->updateMany(7 @! ]+ y6 `' K4 q
  6.     ['state' => 'ny'],
    " o' o. @- F  `. j7 D
  7.     ['$set' => ['country' => 'us']]3 y1 y- M, U6 j: \
  8. );
    : p. w- @0 J2 l. R. W# t
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • 3 h4 ?( U- b  u
  1. $cursor = $collection->find($condition, [8 d; o' `; n+ k4 e# z* T& d
  2.     'name' => true//指定字段8 m3 w( f! M7 d0 I4 R9 o
  3. ]);2 y6 J4 s& C8 c) n5 w" E% Q; u) J) @
  4. $cursor->skip(5);
    6 M$ P2 \' D. P) G1 ^! r8 x
  5. $cursor->limit(5);
      Q1 Z: R! Z/ u7 l& u+ s
  6. $cursor->sort([
    0 J& o+ N7 y. N
  7.     'time' => -14 \: G( J; `: E( I  C! C
  8. ]);
复制代码
  • ) {% x0 ?5 ~1 o( E% k
  1. $cursor = $collection->find($condition, [
    $ B' [- J1 ~7 \" I: g/ C; h) I
  2.     'skip' => 5,
    . \8 X9 Y# F! O6 y4 F9 Z- D) n
  3.     'limit' => 5,
    : w- x7 T: z* c" x: O: h' y2 Z
  4.     'sort' => [  M9 j6 ?. L( ]. L+ @
  5.         'time' => -1
    - T1 @- h6 {. c+ _) E. |) d
  6.     ],//排序1 o, T$ T0 s: q( u3 J
  7.     'projection' => [
      r  Q' u' l. u1 M( k8 b% `8 x
  8.         'name' => 1//指定字段* ^" @) b8 I! L; ]4 u- U) i
  9.     ]5 b7 ~+ ~, J& o; @
  10. ]);
复制代码
5.删除

  • ) E9 V7 w, a+ l1 i8 u
  1. $collention->remove($condition, [/ G, P# ^3 r5 k
  2.     'justOne' => false//删单条8 M8 M7 `' m3 J2 k% x
  3. ]);
    , j. g6 s$ i, F
  4. $collention->remove([]);//删所有
复制代码
  • & C9 r9 h7 K: n' c2 i
  1. $result = $collention->deleteOne($condition, $options);
    + A9 S2 s+ Z! }# O3 \% t8 S, g. {2 F
  2. $collention->deleteMany($condition, $options);' F$ e9 R( L( n4 w5 T

  3. # ?1 G0 P7 e8 C/ B, O3 J" \- s; X
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([2 C4 U5 y  b& T+ a- ?, `
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键) H. ?, u9 w4 _$ s. d
  3. ], [3 Q. ?; D) M8 c/ H5 @; \9 j! X& s
  4.     '$inc' => ['id' => 1]//自增
      ]# S& @. s! @7 R1 ?3 L
  5. ], [
    * {3 o7 M! g# Z! T
  6.     '_id' => 0
    8 J, `. V& @2 W. z: k5 F0 D- u. J
  7. ], [
    5 G/ D; N* m  t5 M. I
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    5 J: S2 H' Y1 J4 A
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    - O# `: k% X. z. d8 m* X1 j( n
  2.     '_id' => $tableName
    + ]% E2 _7 R. T4 a' a* a# ^, C0 k
  3. ], [
    3 b# g, N! i+ i. u
  4.     '$inc' => ['id' => 1]
    ! [% ?! E/ B. ?- B
  5. ], [  S. R. X+ @5 I7 l6 Z
  6.     'projection' => ['id' => 1],; K! a' A2 ~* s  O7 O& l
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    * _! S* J+ h0 ?3 l- d  w
  8. ]);
复制代码

% l; r, E$ S8 t, W, I
& k( E$ F) T; ?$ m, J* Y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 20:50 , Processed in 0.056526 second(s), 19 queries .

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