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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16090|回复: 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) f1 S. f- @8 G7 a5 k

  2. 3 q. K4 ^! Y% d: [
  3. use MongoDB\Driver\Manager;
    . b3 m0 K$ s  S' F5 m
  4. use MongoDB\Driver\BulkWrite;& F& f7 @3 _7 V5 S+ p: H0 K; k
  5. use MongoDB\Driver\WriteConcern;7 b) w1 D: X+ E1 D* @8 K5 C( h
  6. use MongoDB\Driver\Query;9 s! }2 p% g+ i5 z( @
  7. use MongoDB\Driver\Command;
    / K; H# O) R8 ^/ y

  8. 4 D) Q/ J2 C3 p7 y
  9. class MongoDb {% P3 T6 O$ }; v% O) T, L% l) U0 ?+ ]

  10. " k8 |, {# y8 k" s& ?9 |# }
  11.     protected $mongodb;4 K$ w: D$ u7 T1 h% J2 f8 R
  12.     protected $database;
    , W, b' W- \$ R) A4 w
  13.     protected $collection;  P4 x# y% O$ G1 M+ P# |
  14.     protected $bulk;
    % B$ y9 r& y  w* b
  15.     protected $writeConcern;& F3 L$ o7 E3 M0 P5 @
  16.     protected $defaultConfig
    4 B, [, L( @" j' q9 t* V+ L
  17.         = [5 y8 p/ Z: [9 k/ g  j% F& e6 Y
  18.             'hostname' => 'localhost',
    + m7 m$ i0 Q3 ]* m+ O
  19.             'port' => '27017',; T. P+ {' H9 ^
  20.             'username' => '',6 C/ y1 q4 N: D) w9 ~5 h) x
  21.             'password' => '',
    ) Q4 S) l( ^) G" N& }
  22.             'database' => 'test'/ R4 D# R+ [7 P0 t
  23.         ];+ w1 j' [9 _& {8 x: v0 T7 m

  24. 9 t" g6 Q. h8 n4 T( W4 g3 I& \
  25.     public function __construct($config) {# k5 l3 V; b( y
  26.         $config = array_merge($this->defaultConfig, $config);
    1 g* j6 B; r5 N* W7 i
  27.         $mongoServer = "mongodb://";
    % @! A3 J6 g$ @0 [0 x
  28.         if ($config['username']) {1 D+ V; [: H0 J
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
      ~1 U2 a( C. d/ _
  30.         }
    + Q9 d3 G; D5 w6 A
  31.         $mongoServer .= $config['hostname'];  j! N- E  d" c' v, e6 Q0 @/ Y6 I
  32.         if ($config['port']) {7 `) p# K% i2 g- ~) A, P
  33.             $mongoServer .= ':' . $config['port'];
    6 ?- b/ m' y8 t
  34.         }  q* K0 S) q, D6 J) T
  35.         $mongoServer .= '/' . $config['database'];
      I8 B7 B9 n% R; R
  36. " \5 Q: B& a0 A0 M9 Q; ]
  37.         $this->mongodb = new Manager($mongoServer);
    ' q, y2 `$ s9 a5 S5 \8 F
  38.         $this->database = $config['database'];( e) V; q5 b, x- T
  39.         $this->collection = $config['collection'];8 p$ v4 D! A2 }; b. l6 {2 U% X
  40.         $this->bulk = new BulkWrite();! E9 |$ z# `) W6 J
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    7 `" O5 P5 u8 y6 J, h6 B1 D+ Z' f' p
  42.     }
      k* N3 j+ J& _" l: m: m1 b

  43. 5 C+ \# t1 z5 n- C; @
  44.     public function query($where = [], $option = []) {
    . T* i3 @! P! F" }5 s! y
  45.         $query = new Query($where, $option);- y; [5 x# c% j6 Y$ M- a( s' h
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    4 O1 f& O0 C, |& G5 m+ {/ y4 S$ _

  47. + z+ s9 E- g, V. q, J$ v' E5 G
  48.         return json_encode($result);/ R# R! s1 e% Z9 C
  49.     }
    % ^9 h! {- j, e" f
  50. ! Y/ q+ d4 s* R3 U  e6 J% S
  51.     public function count($where = []) {
    ! |: A, g5 h4 I6 B' r7 a
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    % H* k$ c+ M- ?; j: [- e
  53.         $result = $this->mongodb->executeCommand($this->database, $command);" o7 `* O* Y1 \$ U  `
  54.         $res = $result->toArray();
      X# a1 g# c: o! b
  55.         $count = 0;1 f& K! d' T) c
  56.         if ($res) {
    * d6 U: T* L/ }! n- k
  57.             $count = $res[0]->n;
      y1 t. e. T0 v4 B  m& u7 G- o
  58.         }$ z8 P) l, p/ b! t3 y0 z( X7 ^4 j
  59. 1 b: T' L9 P. a" A* `" Y
  60.         return $count;
    4 s) i8 d' t0 M& {  g6 h
  61.     }+ G( }* Z/ Z1 i2 v* O
  62. + u/ G. }' C" N0 e' W  h0 _
  63.     public function update($where = [], $update = [], $upsert = false) {: S, {5 p# R5 s- Z
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    " e+ x% E  f" u
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ) ~9 L1 U! L( l
  66. 0 T2 H; D* R1 ?& P
  67.         return $result->getModifiedCount();
    ! w6 T1 Q- m: i5 N1 D2 L
  68.     }3 P5 A/ F0 l0 k) `4 a' J3 x; n* \0 j

  69. - }* r. C+ D0 E1 c
  70.     public function insert($data = []) {
    0 o1 X, g! ^( d% m# e% N0 J
  71.         $this->bulk->insert($data);3 J0 L2 F  s6 a( }
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    & D- M9 ]4 n' }; \3 M$ V5 T
  73. 8 {0 w6 L* T- S) I  L
  74.         return $result->getInsertedCount();
    2 q) u6 _7 o' y
  75.     }6 S2 G" y* H  r# k7 k7 v' M2 y% w

  76. " w( G; \# j2 r+ N9 ?' y2 q  R3 I
  77.     public function delete($where = [], $limit = 1) {
    5 [; B( m; v3 j& {, `# }4 ]& z
  78.         $this->bulk->delete($where, ['limit' => $limit]);& J0 M" t% h! f! L* r# G9 r
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ' w2 x( m9 a5 f: F1 H
  80. . w7 v* ^  B- j8 ~& f, _% m
  81.         return $result->getDeletedCount();
    9 |3 l, v* R7 I, @( [, x
  82.     }
    , X9 ]1 |: D7 B- ]- x5 k
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • / Y4 ]$ L4 J* v' @5 s
  1. new MongoClient();
复制代码

  • - _! l' C% h1 X" y8 a
  1. new MongoDB\Client();
复制代码
2.新增

  • 8 A3 t% r4 N9 F' B
  1. $collention->insert($array, $options);
复制代码

  • % l  ?& d( ^) P7 Q0 T) t
  1. $resultOne = $collention->insertOne($array, $options);//单# [. B* E7 \* G3 u
  2. $lastId = $resultOne->getInsertedId();3 i4 v7 [+ E' V' _. b1 Z- A9 O
  3. $resultMany = $collention->insertMany($array, $options);//多
    9 i: e; Z2 F! U! ~: y) D
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 3 M5 E; g: H( d( i: A( g% q. u; B3 Q! k
  1. $collention->update($condition, [
    / @9 h, q9 Z: H. e9 C* y: X
  2.     '$set' => $values+ `7 d6 T2 A: f$ j2 O; @
  3. ,[5 o+ l) u* d. a8 y  `4 ~/ G
  4.     'multiple' => true//多条,单条false
    2 s0 ^+ i, A9 S6 ]$ Y
  5. ]);
复制代码
  • ' i0 `8 [' R- E1 M+ K5 Z: M! C
  1. $collection->updateOne(( \5 g/ o: x. ]
  2.     ['state' => 'ny'],; i' L( Z+ z# K+ }4 o$ @
  3.     ['$set' => ['country' => 'us']]& G* T* D4 d$ ~/ v& c/ u
  4. );
    4 X4 F5 D" S7 Y% k1 f: T; j' {0 E
  5. $updateResult = $collection->updateMany(
    " K: L  E: Z* t; ?, i7 ^; R
  6.     ['state' => 'ny'],
    + k$ R. x0 ^( p/ ?3 p; H& h
  7.     ['$set' => ['country' => 'us']]
    8 L/ n8 z; |# j9 B2 ?
  8. );- r/ W# f; ?* _2 c0 j+ J
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • 9 s/ b' \" D4 j8 E3 ^2 K+ t
  1. $cursor = $collection->find($condition, [3 D2 t* D, b& y( ]' A
  2.     'name' => true//指定字段
    2 R% }, Z- c' z7 {- X$ c
  3. ]);+ q- m( x$ i1 k- q
  4. $cursor->skip(5);
    6 P" H2 e5 ^3 D& ?2 ^! B+ |7 u
  5. $cursor->limit(5);
    * C8 w; @; g6 I/ C0 U( Q
  6. $cursor->sort([. ]2 d2 k0 p* k- R+ {$ _8 ^, l' F* J' _
  7.     'time' => -12 L; l& `0 ]: t! h
  8. ]);
复制代码
  • 1 b3 m# B$ G. ^/ i6 R5 A+ K( K
  1. $cursor = $collection->find($condition, [8 P# _4 U) _+ F; u
  2.     'skip' => 5,) K, q9 L5 m+ e
  3.     'limit' => 5,/ S/ k2 [3 I! E) B3 p. ?
  4.     'sort' => [
    3 l) I) i, T/ V, ~! H- t1 F8 H
  5.         'time' => -1* i3 {6 d0 S' y1 V. t  i
  6.     ],//排序" E" C+ ^/ C0 ^; G  ?1 C! I- |
  7.     'projection' => [
    % _6 |& G/ O$ B, u- j3 P
  8.         'name' => 1//指定字段
    ! \0 x6 J; r- K$ m. y" E7 v
  9.     ]
    ) x0 O9 \$ b8 @
  10. ]);
复制代码
5.删除

  • ; y" t9 B' t! L& O4 \( @
  1. $collention->remove($condition, [6 w7 |' T1 C- G; w1 X2 H7 M
  2.     'justOne' => false//删单条2 O, W5 J. m; E9 H* D6 N* @
  3. ]);
    ( O8 z5 @# t  l% x4 U7 q
  4. $collention->remove([]);//删所有
复制代码

  • : R4 p& h. {1 E( c9 g; P3 B
  1. $result = $collention->deleteOne($condition, $options);
    % g- D9 G' S( n5 X" Q" b+ B
  2. $collention->deleteMany($condition, $options);4 `: H0 ~4 f1 n* d$ `& n
  3. 3 [' E, X/ l* W: I
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    $ e  ?* H9 o' R0 q) L  h! H
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键! e+ W0 s! A+ e8 m
  3. ], [
    ! q" G: @2 l: M
  4.     '$inc' => ['id' => 1]//自增
    ' y( g+ c( H- h" j, d
  5. ], [4 t: a; O  }; W9 ~. K
  6.     '_id' => 0! V' Q( R: z& T
  7. ], [
    1 d* y( j- ~8 V: h$ A( @
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    8 z0 M$ p  t! }2 N
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    5 E, F: T6 c4 B; ^7 k
  2.     '_id' => $tableName) N" p" u1 ]/ Q9 V  Z7 @; J( z
  3. ], [! }& ]8 D5 m- v; Q9 S! {0 ?' d3 ?
  4.     '$inc' => ['id' => 1]
    6 K2 m7 x; G& X* k5 u3 T& B; b
  5. ], [
    % C3 F' |; y  R# \) J" C
  6.     'projection' => ['id' => 1],; e0 f8 ^! I  ^6 ?' N4 e6 N$ y
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER8 S$ a' A: ^: i, x0 @
  8. ]);
复制代码

9 H8 l' [! O, c6 ?. b5 l. J' V; u$ x4 n" t
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 18:55 , Processed in 0.064746 second(s), 20 queries .

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