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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11544|回复: 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- p4 [/ `4 `* d' ~
  2. ( O1 y# j8 H. ^' }
  3. use MongoDB\Driver\Manager;- T+ T) W! w6 E
  4. use MongoDB\Driver\BulkWrite;
    4 R" ^7 p- n9 s
  5. use MongoDB\Driver\WriteConcern;
    " ~6 q/ |+ `. Z/ R5 A' t' |
  6. use MongoDB\Driver\Query;1 P  W% e3 [; D3 D
  7. use MongoDB\Driver\Command;( P; `  ?" I3 i

  8. 4 p/ Z; B5 f2 [
  9. class MongoDb {  R& v- X7 X- x: Q
  10. - f3 L" u8 D1 @: g* f1 P9 v  y
  11.     protected $mongodb;
    ; D! v  D) u/ v: R. w1 v% X) O
  12.     protected $database;1 t+ O, v2 g* ^$ _: ~
  13.     protected $collection;
    - d- a/ r5 W5 H9 h
  14.     protected $bulk;
    1 H3 {, |6 U- g9 N# S" I+ c
  15.     protected $writeConcern;( z% K8 j; q: n3 T) t- p
  16.     protected $defaultConfig
    3 O% L# v  J% m! s1 g: ^3 Z1 q
  17.         = [
    + {' O: i! B; E& R
  18.             'hostname' => 'localhost',
    . U$ ^# q( C3 i
  19.             'port' => '27017',/ A! t1 P, G9 c& P2 G2 Z4 y
  20.             'username' => '',6 a8 b) L) ?0 T8 H: l) s' d
  21.             'password' => '',5 ~9 i3 e6 q4 i% a% \: |
  22.             'database' => 'test'- q' Q1 W; K4 F7 E4 A: p/ s
  23.         ];
    3 c7 G: G8 |" d* @! X

  24. % M( i$ A  b7 X1 u
  25.     public function __construct($config) {
    2 }1 V( U6 ]6 q1 c' }
  26.         $config = array_merge($this->defaultConfig, $config);
    $ J% N4 B" N9 b, Y2 O9 H) Z
  27.         $mongoServer = "mongodb://";5 _) _2 Q& I1 W3 m7 j: q
  28.         if ($config['username']) {
    : R6 w5 R; b$ G+ T+ a7 c
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    ( e/ ?" K8 ^( R: O6 H( I
  30.         }) j4 V) E! m+ ~9 |
  31.         $mongoServer .= $config['hostname'];; ?6 e- ?- i/ Q' p
  32.         if ($config['port']) {8 I" v' r' {0 U
  33.             $mongoServer .= ':' . $config['port'];
    + i% o( O3 E$ y, t* I% T' ~! T3 U
  34.         }
    8 ~) u0 `# }* m' N4 A! J/ ^
  35.         $mongoServer .= '/' . $config['database'];
    & C7 J: n$ N; J
  36. ( t$ ]4 _* m& N! N8 t
  37.         $this->mongodb = new Manager($mongoServer);! z) q4 P, E. C' R, @
  38.         $this->database = $config['database'];
    * I8 s" R0 G- g/ B
  39.         $this->collection = $config['collection'];7 M" g3 ~. n& v: B5 t
  40.         $this->bulk = new BulkWrite();
    + S6 P1 r- e3 c/ A6 D
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    * ?) ~* n  ]; k# T9 ^
  42.     }
    $ A6 H9 F8 m2 x/ Z  {& g2 Q1 |

  43. ! p, x( e/ o& w7 p) F
  44.     public function query($where = [], $option = []) {
    8 ]2 Q8 O7 Z, z" B
  45.         $query = new Query($where, $option);5 K/ @: V4 `) D7 o3 h
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);8 S6 O9 ~" s" X4 q7 ^

  47. : x" }  e2 d: B# I! J! R/ z* X
  48.         return json_encode($result);9 v4 u' _* m6 \! N- F/ h
  49.     }
      y7 i. L: Z" ?; P
  50. 8 F% N/ {- @3 _' v& `- `
  51.     public function count($where = []) {
    7 a) \" p7 C# U( k1 b- s, [
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    ( W- r$ z* x0 q  x0 [0 W
  53.         $result = $this->mongodb->executeCommand($this->database, $command);, W7 N: {' }- i" c
  54.         $res = $result->toArray();
    ! H& R0 a5 z! @+ `
  55.         $count = 0;8 _% _+ O/ ?" ^) k- z; F4 z2 ^2 C
  56.         if ($res) {
    7 l- i! m, s" s" P3 e. p4 o
  57.             $count = $res[0]->n;- R2 i$ D6 m8 {2 F) W
  58.         }
    - @% x6 `9 d% [2 p) V% y7 j8 T
  59. : c  W$ m+ f; O- b
  60.         return $count;
    " Z+ _' f" N1 U" A
  61.     }
    % [& B3 @8 n, I% k' T
  62. ; h! s* o9 @) T& w
  63.     public function update($where = [], $update = [], $upsert = false) {
    ( M+ p& j- M( B; A
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);/ b: a# ^  O  M# o7 c
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);" T, J: G$ z7 h% i3 m# y  U  @4 Y9 h6 }; L

  66. 4 h( ]8 P/ M1 m) `
  67.         return $result->getModifiedCount();
    % k6 N/ g& X. t: N, H% d
  68.     }2 Y4 P6 f/ m% ~  J9 V. Y% C

  69. ! Z# }/ a0 I0 `  ^5 u
  70.     public function insert($data = []) {2 `' {/ ~6 t% y5 P, f
  71.         $this->bulk->insert($data);
    ' B4 H) Z" Z0 v& O( J0 E
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);8 g* e# y( g. t# _2 Z% }
  73. 2 n) {/ I: C- x$ m2 U
  74.         return $result->getInsertedCount();& t  e7 D! {0 }
  75.     }3 }  @' E8 R% [, ^8 y( F: s

  76. ! O- ^) ]5 T( f2 o
  77.     public function delete($where = [], $limit = 1) {" G; h7 h7 P+ T) l5 O
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    + @! W" H7 C8 K( J+ L$ d, d
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);7 {' d+ S/ J& }0 y6 [. a& F
  80. 5 |3 V& }9 M! f  U; M
  81.         return $result->getDeletedCount();
    ; c( f% B: B" K
  82.     }
    ) O! Y/ R& \3 Q  V
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接

  • 8 [# q" W0 Q9 [
  1. new MongoClient();
复制代码

  • - r. J) K9 B4 V; P9 h! [3 k+ h
  1. new MongoDB\Client();
复制代码
2.新增

  • $ R) ~4 b  M$ N+ l
  1. $collention->insert($array, $options);
复制代码
  • 3 h- ?3 F' R+ y! N' Y4 a4 N
  1. $resultOne = $collention->insertOne($array, $options);//单9 j8 y$ M& T4 g( ?9 Z# f
  2. $lastId = $resultOne->getInsertedId();+ M, j6 l* S" f' e. b0 u$ n9 U
  3. $resultMany = $collention->insertMany($array, $options);//多, }% i$ b  q+ S3 l, r$ ]2 j
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • ) Z$ w( m8 ~9 T- @
  1. $collention->update($condition, [2 X) q, C: N2 m6 b7 A
  2.     '$set' => $values1 V2 S1 F- I/ N7 j, U1 A
  3. ,[! k# \/ j$ A# Z8 k
  4.     'multiple' => true//多条,单条false
    6 E& n) q3 o( z6 M5 n! F
  5. ]);
复制代码

  • : k; L, u6 b8 c
  1. $collection->updateOne(
    3 \) T0 p6 o8 W' [
  2.     ['state' => 'ny'],+ ]' c" s6 x* r% l
  3.     ['$set' => ['country' => 'us']]" p, {: W5 R4 Y6 j4 A# R
  4. );: s  v: T0 o- J8 l+ K: X) a
  5. $updateResult = $collection->updateMany(
    / U; j7 g  U, j  K2 f( ?/ P- \
  6.     ['state' => 'ny'],
    ) S+ r2 w+ i) H( i- ]( @$ G
  7.     ['$set' => ['country' => 'us']]
    + g$ c  {( Q3 l6 M
  8. );
    # k% @* Y0 e& b) P4 W9 e2 R- n
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • 4 g" M1 }2 g" z/ {4 @3 M$ Y
  1. $cursor = $collection->find($condition, [7 d5 n. H) x6 i  F+ q
  2.     'name' => true//指定字段
    ! }& {/ H1 m" n+ a$ d) H
  3. ]);
    5 R: g8 c- P% f" D) Q; m% h  d# n
  4. $cursor->skip(5);
    9 n4 Q0 I3 |& ]+ Q
  5. $cursor->limit(5);8 a! Y$ j' v2 J+ |; R6 |
  6. $cursor->sort([
    + F, P; a6 Z, w6 x, a7 i7 K
  7.     'time' => -1
    5 o! Z, w% r9 P: Q, _* }
  8. ]);
复制代码

  • ! L! k2 @4 ?& a, w( u7 a
  1. $cursor = $collection->find($condition, [: V& P2 O  n5 B& `
  2.     'skip' => 5,5 A( [: r' X! D1 f8 s  {! l
  3.     'limit' => 5," a8 _* o5 C5 Z: @
  4.     'sort' => [  a' o8 T! O7 d5 N: @: e+ W
  5.         'time' => -1* ]$ N7 _* {; P" r
  6.     ],//排序
    6 M7 P- v# S! o. V, F& B3 G0 T
  7.     'projection' => [1 b- o' s9 ~$ `" x1 L& E
  8.         'name' => 1//指定字段2 G: Y* ]0 w5 U3 ]$ N: v4 |' y
  9.     ]
    6 O& `2 j: d, `) k8 s' w" s
  10. ]);
复制代码
5.删除
  • 6 s+ p8 P, Y- l0 `6 Q
  1. $collention->remove($condition, [( F# o  b4 o, p  n! J- G
  2.     'justOne' => false//删单条- u+ y- O8 W4 o/ }2 C4 Y4 ]
  3. ]);
    - x# d" P/ l/ l  W- \( C% V
  4. $collention->remove([]);//删所有
复制代码

  • - ]: ^! X5 m/ ~/ c4 Y; a8 B
  1. $result = $collention->deleteOne($condition, $options);+ E& w; Y* [  \) m' [
  2. $collention->deleteMany($condition, $options);
    ! f( O" U+ y% D  [3 q/ x& p( y
  3. 5 D9 E& `- q* x% m& }* X  m
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    3 K9 Y2 A% D. \( |% [0 t, \5 H$ M. C
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    , C  N+ A+ n: ]! y
  3. ], [
    $ e7 |! B3 }5 `4 Y
  4.     '$inc' => ['id' => 1]//自增
    ' I& S% O0 _2 D' w
  5. ], [; g2 j0 e( W9 q+ J7 z" @
  6.     '_id' => 0# a$ c  h$ g) \' \0 n7 Z- X  ~
  7. ], [8 W! @) M( W" A) g9 c0 i6 j" s
  8.     'new' => 1//返回修改后的结果,默认是修改前的4 b1 ^( I- l5 S& d9 n
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([2 n; K, f5 Y/ \5 h: _9 s
  2.     '_id' => $tableName& B0 O# H/ m3 b/ `2 h- k  z: _4 v
  3. ], [# K0 F9 M; l5 ]! N+ E" p3 y/ C
  4.     '$inc' => ['id' => 1]
    % @# g: _& I/ d6 J4 `& k
  5. ], [
    - p- q* m! E$ e0 v/ r% d
  6.     'projection' => ['id' => 1],
    % {. |  k, a5 }, g
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER7 `2 m/ \* d7 o
  8. ]);
复制代码
  P$ r4 O# ?5 b1 x, e* y1 U+ j

3 W! v, u  W4 f
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-20 02:43 , Processed in 0.134940 second(s), 19 queries .

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