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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 17002|回复: 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
    % D) r. p: H- }. g+ i8 r0 W
  2. 1 f/ d' `5 B' A* A9 `6 c0 G
  3. use MongoDB\Driver\Manager;
    " [/ F4 l; u) W( H+ I, C) n
  4. use MongoDB\Driver\BulkWrite;
    7 |; g: T% g: a  L4 _1 I# a! m
  5. use MongoDB\Driver\WriteConcern;
    ) M, W! \4 _. v! Y: j
  6. use MongoDB\Driver\Query;
    1 W0 W  n- E  U; g
  7. use MongoDB\Driver\Command;
    / s, A6 |- U! s( u, @/ ]% h9 N8 M
  8. : y  k# ^: C) e; r
  9. class MongoDb {
    ; ]5 f' f3 [' g6 H4 V3 N2 [
  10. 5 j8 ?+ W% w" l& ~. `! w# U
  11.     protected $mongodb;# T2 {+ E$ J4 u3 n/ S, \
  12.     protected $database;& y1 @; T, z6 I/ b4 c5 j
  13.     protected $collection;3 ^4 O% ?& Z, j* U9 y6 U: ?& [
  14.     protected $bulk;+ s3 F! m" F" W7 n" l& Y
  15.     protected $writeConcern;! X/ _0 P- x- ]& O- i: x1 n
  16.     protected $defaultConfig
    : b/ X+ K9 p( P/ U6 x. L; e: G
  17.         = [
    4 {$ G* u- j. g( L2 z1 A: t1 `( R, W3 e
  18.             'hostname' => 'localhost',. |; y/ u! ?$ r7 W: ?% Q* s  _/ ]4 H7 U
  19.             'port' => '27017',' i, _5 c9 W# Y$ S: l3 `
  20.             'username' => '',; v* ]' l& B* _2 ~% _+ J: l& }
  21.             'password' => '',
    8 r, q0 E& B9 l1 T9 N! l
  22.             'database' => 'test'
    % a( y4 H* k  b$ @5 I; n: R9 X
  23.         ];
    ( K/ w9 `6 K' F

  24. 3 Y9 c7 Y! |' l. L/ o
  25.     public function __construct($config) {* C0 Y8 @, W2 g" m/ K- n$ d7 c
  26.         $config = array_merge($this->defaultConfig, $config);
    - V# X$ o3 [6 R
  27.         $mongoServer = "mongodb://";
    $ p- V) N8 s) [; M2 z
  28.         if ($config['username']) {4 B& X2 {- s1 S' K1 o- f
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    # x6 [0 s! a$ k$ i
  30.         }: B6 N$ l2 p  t
  31.         $mongoServer .= $config['hostname'];  [* Y: ?( r; x3 p* b
  32.         if ($config['port']) {, p) O- _2 f" q1 V8 a/ L4 ]- p
  33.             $mongoServer .= ':' . $config['port'];
    9 |0 d. F4 h; T
  34.         }
    6 a0 X, u, n- h
  35.         $mongoServer .= '/' . $config['database'];
    ( B: T- S3 P7 A2 W
  36. % y9 Y% l: z; Z0 y2 F1 r8 G( @
  37.         $this->mongodb = new Manager($mongoServer);8 {; P; b9 {3 ]. N& ?. b
  38.         $this->database = $config['database'];
    6 Z+ n* ?+ [2 {) N; y2 [( a
  39.         $this->collection = $config['collection'];8 z6 O( A4 y+ t
  40.         $this->bulk = new BulkWrite();
    ; ]) Z% M2 v" G6 ]
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
      T, V# f4 p7 s# o1 B0 E) J0 B- y
  42.     }
    4 `$ B! d# m# w' m/ J5 K7 ~
  43. # u4 m; G3 c- O8 [/ N* d" e  q: f
  44.     public function query($where = [], $option = []) {
    8 i+ C9 T# j4 }' H) S
  45.         $query = new Query($where, $option);
    * W# ^' p; X3 E4 @8 k
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);) ~# t! d4 ]6 l6 D3 N) J! G
  47. 6 H9 `3 S& X7 F( Q0 F% e* G- S/ d
  48.         return json_encode($result);
    : B4 n# i( g( j5 C0 E
  49.     }4 t: O4 P/ n; A

  50. ( j; n8 g8 t, d( Y
  51.     public function count($where = []) {/ P. e& h/ K+ N2 ?# D5 f
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);* r4 `2 n$ q4 }8 n( _& ^
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    ' ?4 E- F) I' x0 V7 c0 U
  54.         $res = $result->toArray();4 y$ K0 {8 T  Q: l- u) R
  55.         $count = 0;
    7 }2 m  z8 l- s( O! n/ t( `/ k
  56.         if ($res) {1 M. d$ t, a: a$ ]$ J2 {0 G5 v4 ]
  57.             $count = $res[0]->n;
    ) a; H$ ~( A. M3 ^
  58.         }
    9 z: I4 f6 V5 [

  59. % I, f" E( q3 M6 e
  60.         return $count;
    & H+ L, F% p, N5 e4 h7 T' F# [; }
  61.     }! P& L' Q- F7 N
  62. 1 D* k6 a' ~0 o# |8 q; P
  63.     public function update($where = [], $update = [], $upsert = false) {- U$ |/ `% z5 I
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);5 r6 ?$ I4 H$ i1 c
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    0 E$ Z; D' b8 V' n- k$ h

  66. 4 P! m0 @& w: W$ n
  67.         return $result->getModifiedCount();
    ; ^7 g+ ~* J7 t3 ?) @8 J
  68.     }  G/ @% g, x! K+ m* X9 \2 Z
  69. 2 D/ L# D) o8 U- T+ l! S9 ]; n. r
  70.     public function insert($data = []) {
    ( W2 t( z" ~# A/ c$ x: h  a  W
  71.         $this->bulk->insert($data);/ h3 N7 T8 [+ r
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    4 F& ^' A. b" w. y+ X; a6 d

  73. 6 V0 T/ O- S: G0 U$ U7 @; F
  74.         return $result->getInsertedCount();$ v" c; e6 V* g% {& H3 u* D
  75.     }
    9 e0 H2 }9 E% C# Y( \* R
  76. 4 }5 `, V! T+ Z: T( k3 X
  77.     public function delete($where = [], $limit = 1) {
    $ x/ b! @) o0 ~! }0 [+ o7 N; F
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    - ~1 `8 p, E0 D" r; Z; d  o1 O+ N6 e  ^
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    9 E5 _3 a4 y  I
  80. & }: F: M# R& B0 [. b: S! ]
  81.         return $result->getDeletedCount();
    5 j0 c  v  t& S  A
  82.     }2 p/ @& t) M3 p( N9 U. R
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • 0 h% V/ ?5 r# f/ y2 V/ q
  1. new MongoClient();
复制代码

  • : z  J. o* V0 e' `! h) F) a" A
  1. new MongoDB\Client();
复制代码
2.新增
  • , I: t  k, u7 \% j& G; `' L
  1. $collention->insert($array, $options);
复制代码

  •   E4 d, C! d+ \0 }( ^  r0 p/ v: [
  1. $resultOne = $collention->insertOne($array, $options);//单
    % w5 W9 X7 ~- O
  2. $lastId = $resultOne->getInsertedId();8 a6 p$ j, Q' y6 d5 b
  3. $resultMany = $collention->insertMany($array, $options);//多
      `  W$ u4 w/ g8 m7 x7 @
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • + d$ j2 y6 ~3 ]
  1. $collention->update($condition, [
    1 n  C0 \. N9 B; ?; r8 ~
  2.     '$set' => $values
    1 d& R% x/ |; p# L
  3. ,[" x( r* O( I, ?& C% o8 N# m. w
  4.     'multiple' => true//多条,单条false4 A. L' B$ c) J2 ~
  5. ]);
复制代码
  • , q) o* N. {/ P6 {0 B* Y! @; U
  1. $collection->updateOne(
    $ w4 [8 A' Q' q
  2.     ['state' => 'ny'],2 |! x3 k7 q4 B/ c8 m! f0 k
  3.     ['$set' => ['country' => 'us']]: o  p2 {& Y& b+ X8 [, ?5 }
  4. );) T8 N! @# A6 L9 h" h
  5. $updateResult = $collection->updateMany(- A' z! G% y1 C; J# t
  6.     ['state' => 'ny'],) ~% K" F9 V) U3 K5 g; w
  7.     ['$set' => ['country' => 'us']]
    " h" |1 {/ U' k- o; G
  8. );6 c) l  ^, x2 }" J2 `2 T6 `
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • % o5 L( v" L+ ^2 }$ T* e# C6 t2 ?
  1. $cursor = $collection->find($condition, [
    + C8 m) x/ L3 V( j" e4 l
  2.     'name' => true//指定字段
    4 |3 A) J$ ^# |( q" v
  3. ]);
    % `! M: H# [( U6 O0 J
  4. $cursor->skip(5);
    % W0 p3 z9 B/ b. B* u7 q5 e  g' j
  5. $cursor->limit(5);8 u: F4 Y  Y$ t3 R& S" f! l
  6. $cursor->sort([- K) a7 F! ^. g2 A1 f
  7.     'time' => -1) F& s" C- A9 @7 ?: |, J$ X* q
  8. ]);
复制代码
  • 4 k" c0 \9 n; i2 E
  1. $cursor = $collection->find($condition, [
    # o, J3 i5 U5 |  U6 @9 h
  2.     'skip' => 5,
    & ^& r1 ]$ S- e" f" N5 C$ K# j# D
  3.     'limit' => 5,
    ) U7 P3 `- D4 i6 j3 |/ S' T. V
  4.     'sort' => [
    ; N: F! L4 N( g5 j" l
  5.         'time' => -1
    3 S, |0 O. {& D1 X
  6.     ],//排序
    ; H" r3 w& j; w  t1 h5 i! q: L/ t
  7.     'projection' => [
    . G2 Z. G! y, K+ \8 r; N, ~. Z8 I
  8.         'name' => 1//指定字段% }0 D9 c% L# [- S6 a* J) k/ R
  9.     ]
    ' F4 B+ h  O* f( w, Q4 {
  10. ]);
复制代码
5.删除

  • $ ]  ^9 y1 m3 N7 |0 }% u9 A
  1. $collention->remove($condition, [
    , n) H8 n& J) o/ O
  2.     'justOne' => false//删单条7 X: h0 N& p! t. O, ]
  3. ]);. q& ?5 J& R- V
  4. $collention->remove([]);//删所有
复制代码
  • 8 Z- x( ~! Y2 x9 Y
  1. $result = $collention->deleteOne($condition, $options);
    1 [/ K4 Z$ }# v% m
  2. $collention->deleteMany($condition, $options);
    - t" o; |) f  T5 U; m# t! f

  3. 8 _- k5 d0 u/ S  I2 [; G+ X. y
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([" S# g! u7 N5 C2 S$ Y  {3 e% h, I
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    ; c8 T  @" p0 N- j3 \1 Q3 _
  3. ], [
    ' A& \% C3 W( @$ e. T/ R6 l) v
  4.     '$inc' => ['id' => 1]//自增) G' `; a) }$ @( C. f7 c& o
  5. ], [
    4 R* g* ^1 Z. Y4 d
  6.     '_id' => 0
    % k. L" Z0 l; y( M/ \
  7. ], [
    ) h/ T" ~$ Y0 w/ F9 L! @
  8.     'new' => 1//返回修改后的结果,默认是修改前的( V6 ?: n  m( h- ~! t9 \
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    . `& w) W' {, f) V% _+ j
  2.     '_id' => $tableName3 h3 l7 A/ J4 i; h5 O* S
  3. ], [. C" u( ]9 M, u. M3 q
  4.     '$inc' => ['id' => 1]
    # k- `2 w0 C8 z" n% }. `+ I
  5. ], [& \# D3 C$ z& _8 ?' s) A
  6.     'projection' => ['id' => 1],# w& ~6 E2 r% @( |& I( e
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER: ]7 S5 X# K% a  f$ j/ @
  8. ]);
复制代码
1 K# s' F  J* @4 @' d

* ~; J, y5 m" n+ q: v5 Z; @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 10:51 , Processed in 0.066196 second(s), 20 queries .

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