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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16365|回复: 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
    & E& c: i% l+ {) e1 k
  2. 3 ~2 u. B& D5 O! n
  3. use MongoDB\Driver\Manager;2 K( E7 `5 `0 h/ V* l
  4. use MongoDB\Driver\BulkWrite;7 O7 b3 `( Z/ P3 E) e! m0 q
  5. use MongoDB\Driver\WriteConcern;( @' h9 l& a& [8 p. C% P
  6. use MongoDB\Driver\Query;
    ' q1 g' c6 E; T7 x( G
  7. use MongoDB\Driver\Command;
    # ~) A; @2 x2 x% O' o* g
  8. 7 A! ^" ]2 O6 R0 m1 \, t
  9. class MongoDb {0 j9 Z$ n2 _& y! R- r

  10. # u# R( T$ A( ]% V* T
  11.     protected $mongodb;
    . j7 I4 p+ u* ^; H. m0 a
  12.     protected $database;
    / _) v* f( F% ]
  13.     protected $collection;$ ?0 b1 M5 d5 I! U+ G5 u
  14.     protected $bulk;, G$ s& F9 Y- I; t1 A/ @2 a
  15.     protected $writeConcern;
    & \4 E$ c4 x8 ]* o4 L& m, {
  16.     protected $defaultConfig% c  O( `2 b4 y. L
  17.         = [& [4 @3 h1 b4 a* r; x& q
  18.             'hostname' => 'localhost',
    % d. c/ f% d! i- `
  19.             'port' => '27017',: s6 ?0 P0 C( j8 b# h
  20.             'username' => '',
    * N/ Q7 Z; E4 Y. D( E. ]% ]. ]
  21.             'password' => '',# P" C6 c+ ?1 Z
  22.             'database' => 'test'$ Y1 x: X) [. S, m+ p% k8 p
  23.         ];; ~4 }8 r  }/ P; N- a
  24. * O2 O) b9 ]- x# D0 k# t7 Z& X
  25.     public function __construct($config) {6 ~. B2 ?* ?4 A
  26.         $config = array_merge($this->defaultConfig, $config);9 s; K* B8 [/ D7 U/ K- N1 @
  27.         $mongoServer = "mongodb://";
    ' `# d/ v+ a. ~4 a4 w/ g
  28.         if ($config['username']) {0 |* d1 A1 a( w" A5 W' e
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    2 ^, l. ~  `% e5 x
  30.         }
    4 [. w/ r) x* ?3 e; J8 ~$ B  n
  31.         $mongoServer .= $config['hostname'];- ]$ s# [$ U: J2 J# H) Z
  32.         if ($config['port']) {: J# i8 j6 T. e: b
  33.             $mongoServer .= ':' . $config['port'];
    * o5 |- N! r* [/ B! y, w3 J
  34.         }
    3 J1 s) ^+ l. G/ r- f
  35.         $mongoServer .= '/' . $config['database'];
    1 R$ z. U& n/ R+ s
  36.   {5 I3 h  F9 G/ q$ T4 j
  37.         $this->mongodb = new Manager($mongoServer);
    ( e  f$ M. U2 Y& ]1 v6 a) _
  38.         $this->database = $config['database'];
    ) A& d. {$ M' I# p  s7 G' Z8 `
  39.         $this->collection = $config['collection'];
    % E$ y- d$ O" W! Y/ j/ T
  40.         $this->bulk = new BulkWrite();+ o; n  `- J/ P* X  L/ O. f
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    ' D7 B: Y0 j, ]& v5 ^2 f- N6 M
  42.     }
    6 _+ G2 E0 Y, [  ^0 y

  43. 5 i2 C1 Z) f+ S( M  R
  44.     public function query($where = [], $option = []) {
    $ [/ y1 x% K. ?: a: G7 [- D; E
  45.         $query = new Query($where, $option);
    # [9 [0 Z+ \6 U# @5 t
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);% s/ j7 f& _1 V, u9 N
  47. ; `. i; x! J- Z
  48.         return json_encode($result);( t0 l! n" |% x5 p2 d0 O
  49.     }
    ! N3 c: z! c" t4 f
  50. * d  {1 I% S8 l7 V1 x
  51.     public function count($where = []) {
    5 `4 o+ m7 ^( Q7 M& b
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);2 {  N9 C+ }* H
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    - o% |# d9 f- V) c
  54.         $res = $result->toArray();9 B; ?4 L$ Z) `% \- N1 H8 N. u1 w
  55.         $count = 0;7 J7 {2 e# N  G" o0 _1 p* V
  56.         if ($res) {
    8 @/ ?7 [* z0 J3 n' x/ E
  57.             $count = $res[0]->n;+ z! B+ B! U8 W
  58.         }+ F  |0 H& P/ m  I" n3 n

  59. 7 I& h. q$ a( i& Z5 O( B: E
  60.         return $count;
    - x! X6 E$ ~+ g! T- @; C
  61.     }
    ! V- o* S" a' S7 X% C( z/ M% Q5 n

  62. 2 B8 |  R. C/ }0 s: @, n7 D
  63.     public function update($where = [], $update = [], $upsert = false) {: l( t$ `0 {( R1 c5 J' z0 {
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    ) i1 d- d8 e3 A& x7 ~6 r( }0 N  A
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);# p* p' \- r2 G( h

  66. " m7 a% \  q# b8 X: x$ ?
  67.         return $result->getModifiedCount();
    , {; |3 i0 E% t
  68.     }
    - J' L6 [" f" U/ H* A
  69. 9 {: |6 {. U! _& M8 u
  70.     public function insert($data = []) {2 n4 S6 F- g" z3 F3 s/ V
  71.         $this->bulk->insert($data);
    , v. b, P. x6 {7 ]
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ; [2 W* D" O9 S/ l: @3 ^) {

  73. % P  g/ V% K! E3 T' U8 ^1 y
  74.         return $result->getInsertedCount();( P2 W9 f& E  |7 ]8 e3 N5 ^( y
  75.     }
    2 P7 K( S8 D/ n
  76. + \, f+ @0 \: ]0 P& c
  77.     public function delete($where = [], $limit = 1) {
    * s" _1 ]) S  A( V
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    $ ~3 k8 C" c: l4 v
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);0 I" p, Q& Z! ^$ F5 h

  80. 2 E+ P% W3 Z0 d, @
  81.         return $result->getDeletedCount();+ ^* ~& l8 E+ ?; g0 p
  82.     }
    " T8 w6 k9 M) b0 Y
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • / {4 L9 ]  l! n" O) U5 x, T, D0 M
  1. new MongoClient();
复制代码

  • 5 ]9 H1 S9 \, `: E
  1. new MongoDB\Client();
复制代码
2.新增
  • 8 W! ]5 h) H  f, x# t4 D% d% \
  1. $collention->insert($array, $options);
复制代码
  • ) h- A2 R5 v( t& v
  1. $resultOne = $collention->insertOne($array, $options);//单
    - u& w, A' z" W& B( ^* j4 B$ o$ {
  2. $lastId = $resultOne->getInsertedId();& ?3 ?0 J2 l$ E) ?) N1 ?2 p, |
  3. $resultMany = $collention->insertMany($array, $options);//多6 Q( p( {7 J% K/ d' d0 U" ^0 n: ^
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • $ O+ J- R1 L1 r3 V! a1 I3 N5 V! m
  1. $collention->update($condition, [6 B. V$ c: K1 g+ C5 q1 L
  2.     '$set' => $values+ f2 s5 s$ p6 e& p. J" @
  3. ,[, ~+ m# e! Z& T& x7 Q: c2 r
  4.     'multiple' => true//多条,单条false! x; ^( n, B$ \; H" Y; r
  5. ]);
复制代码
  • " t. s+ T  u" r4 D
  1. $collection->updateOne(
    % w8 b6 f! H8 W" D- G
  2.     ['state' => 'ny'],
    " [# W: L8 g4 Q) x& I* Z
  3.     ['$set' => ['country' => 'us']]
    1 f3 M4 h$ a- J' _0 l6 D$ h! w
  4. );
    - Q0 |, l3 m8 }- }5 t. T  w
  5. $updateResult = $collection->updateMany(
    6 b: j4 f  R" H) B0 m
  6.     ['state' => 'ny'],
    2 ^3 |- a4 c0 D9 ^0 K
  7.     ['$set' => ['country' => 'us']]& U( A, c, E: o: A, b
  8. );
    ( `- ~! X6 v/ Y
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • ( ~( M; p1 r6 d# {2 Z% Z
  1. $cursor = $collection->find($condition, [+ b0 Z2 P, L) X: `( \) g
  2.     'name' => true//指定字段
    / \0 J* c# _! t# ~9 c
  3. ]);
    , N* W2 y. J9 b
  4. $cursor->skip(5);) S9 y2 B  Q) ]
  5. $cursor->limit(5);
    & ?; t7 \6 U. u7 f1 o2 L
  6. $cursor->sort([  `0 j0 L1 O# ?0 X
  7.     'time' => -12 r2 n0 x  e; w$ q
  8. ]);
复制代码
  • ( v% j7 k) |& S
  1. $cursor = $collection->find($condition, [! h8 f' b+ ?7 J& d' _0 n
  2.     'skip' => 5,! u2 |- A$ K" O* V9 c; l' t
  3.     'limit' => 5,
    ) ]0 A$ E8 E5 J$ @
  4.     'sort' => [
    4 |- D1 \+ p! H' p# P* T6 o
  5.         'time' => -1
    2 L2 W/ ^$ e9 i# ?- U4 K  V
  6.     ],//排序8 F6 F7 h( l- C8 M; ~7 H% M( X" ]
  7.     'projection' => [/ M! h8 [! @2 `
  8.         'name' => 1//指定字段. Y; p6 Q& V$ f$ {0 n/ ^  [
  9.     ]) K  `2 J+ o5 r4 s$ W+ S* O4 t
  10. ]);
复制代码
5.删除

  • 0 F) E) Z* z$ t! F2 i
  1. $collention->remove($condition, [
    2 K; L" \/ e5 L* {5 b9 Q
  2.     'justOne' => false//删单条
    " {& x& M7 e2 E- M2 b$ p  U# N( W, X. |
  3. ]);& s5 E8 M% X3 b% e/ ?, R  R
  4. $collention->remove([]);//删所有
复制代码

  • % y) z& E3 f; |( Z# |6 Q8 }" X
  1. $result = $collention->deleteOne($condition, $options);
    2 g( U) N  R9 A
  2. $collention->deleteMany($condition, $options);) w0 B- K5 u9 H% p1 F- R8 N% Q
  3. , _: H2 f1 |3 I) i, i
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([! V1 `/ x5 [7 A' K* C
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    + F0 V" ]( H! R: n
  3. ], [6 R) J6 t# G& l* b  J' L6 h
  4.     '$inc' => ['id' => 1]//自增3 o  ~, j+ S0 G& ^  n
  5. ], [% I- M+ w- w1 a' z, b* S/ I! e) u6 i
  6.     '_id' => 0
    & o, Q5 `. B) @- c. t% \, j5 P
  7. ], [/ z' ~. ^) `; N! L( {. H
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    # ?, p, R& M% M, R* v
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([  F! L' j; {/ P5 ?0 G& {* w
  2.     '_id' => $tableName
    & h8 L/ ^: @4 b7 G9 H! ?
  3. ], [
    # j& X0 Y8 `; n% [) [
  4.     '$inc' => ['id' => 1]; @8 N- c5 S) [9 K. V# L' L
  5. ], [+ t0 `' Q6 E% w+ n  @/ E
  6.     'projection' => ['id' => 1],
    7 k* m! S# `) |( ~, \+ V' L8 K
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
      V+ L. R9 I) s
  8. ]);
复制代码
1 r) H% p9 f1 w% g4 Z+ [. ^/ c
( W! r( `* ?8 P  [! v4 K) \7 [
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 15:29 , Processed in 0.062323 second(s), 19 queries .

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