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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 15966|回复: 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. <?php8 O: @6 g  d0 B7 |1 M2 T# O
  2. . L& Q$ z/ P1 w+ N, Q
  3. use MongoDB\Driver\Manager;( T, T. _/ t7 `) T7 _9 E% ?. u5 {
  4. use MongoDB\Driver\BulkWrite;
    - [* E. Q" s9 t' L
  5. use MongoDB\Driver\WriteConcern;4 X9 ^- S% G; @, e/ M
  6. use MongoDB\Driver\Query;
    ) i; G! b$ _& `, w: U
  7. use MongoDB\Driver\Command;
    - z7 K# b5 W7 @2 O0 `$ G0 {

  8. 7 f1 F$ g( C! ^& G$ s! Z. q% b
  9. class MongoDb {; i7 P# C; @0 Z; U$ U
  10. + F8 i0 O; O9 w- u4 g! |
  11.     protected $mongodb;
    , G0 W6 X, L6 F: A3 D
  12.     protected $database;6 @. G& c4 e6 k, p  A, A9 [; U
  13.     protected $collection;
    + N9 u5 i1 O3 [* `- J6 K
  14.     protected $bulk;5 T' O. {4 e7 g
  15.     protected $writeConcern;
    1 {' l7 [+ A  ~! ?# o8 z
  16.     protected $defaultConfig
    8 k. s( I3 e" ~
  17.         = [6 v0 u1 z) p; R: M- P$ `
  18.             'hostname' => 'localhost',# F( b9 M6 y- f! ~& s( f. F
  19.             'port' => '27017',) I0 g7 ^! S& Q( ~& Q0 K) E  j- ~
  20.             'username' => '',
    0 r- f6 e6 ]! Q' C$ o* F4 Z  ~6 W" o
  21.             'password' => '',
      Q" a3 n! Y/ }+ G* J$ k
  22.             'database' => 'test'
    % Y. U: Q9 t) }+ k" W5 S& U' M
  23.         ];5 I2 D, d8 A' ^/ I5 [/ I
  24. ! \9 t7 \! F- B* J: ~- d9 j8 e
  25.     public function __construct($config) {# \/ Z7 H; z+ O" P$ |( S" j" F2 y6 J
  26.         $config = array_merge($this->defaultConfig, $config);
    # x. s7 s( |' Z
  27.         $mongoServer = "mongodb://";8 Q0 |9 I/ x9 ^( F1 X5 L" _3 t" H
  28.         if ($config['username']) {
    $ g5 e# ?) F8 j" |
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';( z1 U& S  b7 z7 E# J2 u
  30.         }! \' v3 z0 j6 Y6 i4 B4 |! G
  31.         $mongoServer .= $config['hostname'];
    # D  _; z7 V* ^
  32.         if ($config['port']) {
    0 g1 L0 Q  Q/ d3 a2 V7 V
  33.             $mongoServer .= ':' . $config['port'];
    - v5 r" Z6 C/ U4 x3 D9 N$ ~
  34.         }0 R7 P  f7 v3 o, J$ t, S& y
  35.         $mongoServer .= '/' . $config['database'];
    * x9 y  n, Q# }$ n( f7 x. @6 y
  36. 2 @+ _6 u; c" ^: l
  37.         $this->mongodb = new Manager($mongoServer);& q) o+ u: q' V9 d% |% Z1 }
  38.         $this->database = $config['database'];  Q$ o- b$ D% E, F; H$ X
  39.         $this->collection = $config['collection'];
    : W6 d* p4 o# x9 K  J
  40.         $this->bulk = new BulkWrite();
    2 L7 N9 r: M& G. r$ R+ K
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);! |1 o0 ?7 i, A% }0 Z+ c( J( W
  42.     }! R1 I2 O0 I9 `6 l9 W4 h* M3 g/ z/ V

  43. 2 e7 e. L% y) }, s7 v' A" `
  44.     public function query($where = [], $option = []) {3 b" a- n" Y# d; k  Y& t
  45.         $query = new Query($where, $option);
    + n2 ^$ i) ?, |$ s9 C' q
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);2 t% i: }$ F; `( M5 O9 J
  47. 9 b1 P4 S2 K/ z
  48.         return json_encode($result);
    8 p. ?# Z+ d1 ^
  49.     }6 p0 T5 m/ y: |4 F* p

  50. 5 e; I3 p( x, `; _) z. p
  51.     public function count($where = []) {
    ' }! n' s: v5 @" z) g/ V
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    9 c% u/ m. T: H. C$ F# m. |" [
  53.         $result = $this->mongodb->executeCommand($this->database, $command);! L5 p9 n1 G0 [; C! j! }3 X
  54.         $res = $result->toArray();3 j& _8 f; F3 O, a7 f: E. n
  55.         $count = 0;' S1 A7 D4 A* {6 s8 a) D6 |$ h0 U
  56.         if ($res) {
    4 x! o7 f; q+ _1 |  P" Z$ d
  57.             $count = $res[0]->n;9 m& t% i( Q1 X3 b& O. l, o
  58.         }' a% j, B+ p) O% z. M0 q
  59. : L, I+ P! q% t5 Q7 w$ F6 U+ W
  60.         return $count;: s3 }$ X! l7 w! u! A: a+ S, p. U
  61.     }1 ^) x5 e; o  e
  62. ) J( X: ]" v& K4 K0 A8 N; }% E
  63.     public function update($where = [], $update = [], $upsert = false) {3 F5 g* N3 W% t' R
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);) A, |7 V) v# K3 A: N, S
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);' w4 O  T" P% \# v+ R  }
  66. 6 W' x+ q& f  n2 h+ g, T) P
  67.         return $result->getModifiedCount();
    ( S3 l/ Q: E6 o7 L
  68.     }
    : v7 d" \5 X4 [$ Z7 h4 D" P& S# V

  69. 4 h1 z4 S* U+ ]; z
  70.     public function insert($data = []) {
    8 ?8 [8 ?; l7 a  h
  71.         $this->bulk->insert($data);
    3 H4 c3 W- j% t6 ]* ^' j8 G
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ' w1 J# j0 L6 {! I+ Y$ U9 [

  73. 4 ^% O- P, s  L/ y
  74.         return $result->getInsertedCount();
    : P) N! h0 t& Y
  75.     }6 E3 W$ ~+ y4 ^! D/ ?
  76. 8 R8 x+ p. e# K" W# ]& M" U2 Q
  77.     public function delete($where = [], $limit = 1) {7 h! t' ?" N6 j6 o- c
  78.         $this->bulk->delete($where, ['limit' => $limit]);4 ?/ m$ Q" m% Q& T, T2 ~
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);. ~2 p( G8 d% P8 n
  80. / Q, I$ S* r" G4 Z
  81.         return $result->getDeletedCount();
    3 z1 s% I7 N0 e( b7 S! J9 p
  82.     }
    0 M) |. @& o( v9 F
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • 1 ^" ^( p. Q2 t! Y; M4 i1 s
  1. new MongoClient();
复制代码
  • ; w2 E0 @/ `! u! V, c
  1. new MongoDB\Client();
复制代码
2.新增

  • 4 {% }$ M7 F! u/ q, e8 R7 N, ]1 G- G
  1. $collention->insert($array, $options);
复制代码
  • 7 c0 @4 S8 |6 C& H5 G5 K* v; D  ~
  1. $resultOne = $collention->insertOne($array, $options);//单: m0 l" U0 V% n4 p% A! r8 Q
  2. $lastId = $resultOne->getInsertedId();
    5 N. t; I4 R" v# Y1 Y. e- C
  3. $resultMany = $collention->insertMany($array, $options);//多) `" T" Y' T, g) o1 ]
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • . `$ v/ i/ o! H2 [) w
  1. $collention->update($condition, [1 s: `' Q# w, U$ Q: l! d) X$ Z
  2.     '$set' => $values
    : n$ i0 S- U3 [  m% r. f8 J
  3. ,[
    * h: z( y' U& k* L: R
  4.     'multiple' => true//多条,单条false
    7 |7 H( D, L+ J* H/ G' S' Q
  5. ]);
复制代码
  • 0 \0 v2 {, t! X( w+ R
  1. $collection->updateOne(
    % K( p; @3 F  G4 U
  2.     ['state' => 'ny'],
    ( O7 V( M% w, r1 }8 A- a# U
  3.     ['$set' => ['country' => 'us']], E5 A) A# q2 q8 ]
  4. );6 |/ D/ _6 J5 q: D, G7 f/ c
  5. $updateResult = $collection->updateMany(
    % x2 J9 ?2 c+ L9 Y" o
  6.     ['state' => 'ny'],
    * C" {/ b5 n: E* @
  7.     ['$set' => ['country' => 'us']]4 b4 p7 }+ C* `& _4 P7 m4 H
  8. );
    & i( }) r' c1 R3 L. R
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • % g' E: E- I" z6 ~+ o
  1. $cursor = $collection->find($condition, [/ A* C1 B; q/ X
  2.     'name' => true//指定字段# J6 l& L4 F) R5 n6 A0 @  f
  3. ]);
    ) i, G9 m3 s) A
  4. $cursor->skip(5);
    6 g# J+ o% F$ t  W. l
  5. $cursor->limit(5);
    ! F+ D+ j* W; Z! D4 J* S
  6. $cursor->sort([' u4 O! M- N) O$ {) m1 j
  7.     'time' => -1
    , b# t$ x5 C. b7 ^3 N! J, _4 n
  8. ]);
复制代码
  • $ k3 z$ B* v) q
  1. $cursor = $collection->find($condition, [" w( E6 ?$ X/ |2 y; B& p, ~
  2.     'skip' => 5,
    & w5 F' u" S& ?: R5 H
  3.     'limit' => 5,
    , |7 `) X1 d4 C7 _' h" M. N7 N" h# `
  4.     'sort' => [
    & z0 F. G( f: h8 |" a- e
  5.         'time' => -1
    ; V( c% H: {6 |4 K) H
  6.     ],//排序$ }! |$ B, @9 p% Q* N0 z# ~
  7.     'projection' => [- Z4 ]8 I+ c  D9 y1 V5 x
  8.         'name' => 1//指定字段9 w/ E# ]9 i& |  W# }% l
  9.     ]
    ! r. @& `# C- N3 N
  10. ]);
复制代码
5.删除
  • 2 d  a) N2 x+ U+ z# Y" l" E( O, P
  1. $collention->remove($condition, [; s& H. |; j1 S
  2.     'justOne' => false//删单条
    ! |, v5 F6 o  j* u
  3. ]);
    3 j  p" N; O7 r1 R$ X* p- ]) a
  4. $collention->remove([]);//删所有
复制代码
  • % o. F5 R0 K5 j) y
  1. $result = $collention->deleteOne($condition, $options);/ ?5 O2 y3 I7 ?  L' q$ ]/ A
  2. $collention->deleteMany($condition, $options);/ f( p% x* p  m1 P7 M- e% j
  3. 2 Z/ q  Q: p, g  Y5 n1 _- O9 ]" ^
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([  `4 l% F8 w# w) t
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键8 I% |! w4 L1 C* T  ^0 Y4 {
  3. ], [
    + w& Z6 [' N9 K0 q4 a1 L
  4.     '$inc' => ['id' => 1]//自增
    % V6 p) O( J3 X6 a+ U3 P
  5. ], [
    0 [7 O8 F% t, g: i) Y
  6.     '_id' => 0
      R) u: Q5 n3 _; U5 S
  7. ], [
    8 x  |) x: I% y4 _* d" r
  8.     'new' => 1//返回修改后的结果,默认是修改前的* o" G8 @; T1 E1 ?* y
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    ; d! D- c' I1 S. V  [
  2.     '_id' => $tableName
    , S3 y3 G5 X! J- `3 K" c( w! {
  3. ], [
    5 J: S  t1 U/ y* P$ d( |  t7 ^
  4.     '$inc' => ['id' => 1]
    0 _. C& U+ P/ r" E2 \
  5. ], [
    + V6 W8 Q9 V# ?/ N7 _
  6.     'projection' => ['id' => 1],# v+ \" J* R3 s/ K
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    6 y& A3 I' p) X$ E6 Y6 F
  8. ]);
复制代码
0 R9 g; M! M% t( Q
- W1 C) q: n) U5 x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:00 , Processed in 0.094852 second(s), 27 queries .

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