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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 15967|回复: 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
    . o4 e/ D. r, m  R4 {
  2. 7 e( Q7 C" ~* w
  3. use MongoDB\Driver\Manager;
    ) s+ q( G+ L2 p- R: [5 Q
  4. use MongoDB\Driver\BulkWrite;
    + Q/ f: r) N4 }' Z. h
  5. use MongoDB\Driver\WriteConcern;
    2 w8 T3 B6 K' P3 |2 }& Z
  6. use MongoDB\Driver\Query;
    , k% E: P( Y$ s$ h/ S# s6 k
  7. use MongoDB\Driver\Command;
    % Z) E- M% C! @* c
  8. & n+ H  V3 c) Z% ]7 B7 \
  9. class MongoDb {0 H- K+ q! w/ B  z# W
  10. 9 ?- H  f$ M* m" P4 x& O/ z9 D
  11.     protected $mongodb;# p  A, z( t) \
  12.     protected $database;. j: g* f3 k  Y+ l) Z- o
  13.     protected $collection;
    . L$ \) U0 J& ]8 d0 K
  14.     protected $bulk;
    % |' G6 P! p8 z6 P2 r; L
  15.     protected $writeConcern;
    1 W* k: ^' I+ b+ r. \5 y: I
  16.     protected $defaultConfig3 [2 E) f% z! q
  17.         = [5 ~" d8 v* Z8 y' _* T
  18.             'hostname' => 'localhost',
    3 [. G$ x/ U& s  b; h
  19.             'port' => '27017',' x) y4 F# Q# e; N  l! ~
  20.             'username' => '',9 Q4 {6 L( m) C: q
  21.             'password' => '',+ B2 _. W( q- l1 J' W& ]1 X
  22.             'database' => 'test') j( q5 h( e2 k0 N5 ]2 S- N
  23.         ];. n0 ?% z1 {( A) W7 u- `3 e

  24. 7 _5 H" Y7 c1 c  H, ?
  25.     public function __construct($config) {
    - V! ]8 C3 T! D; L8 m1 Y
  26.         $config = array_merge($this->defaultConfig, $config);
    4 z  E6 b: r4 m
  27.         $mongoServer = "mongodb://";
    2 }/ ~- v! V  S6 m: o/ f
  28.         if ($config['username']) {
    . X& \8 f! y# ~0 ~
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';! L) K1 S$ l- x3 y6 b
  30.         }8 i( K2 J/ d! F' K& ]) [! p. b: a
  31.         $mongoServer .= $config['hostname'];
    0 ~* ]/ w6 t' H+ H- S
  32.         if ($config['port']) {
    , H/ q  M# j, c% J1 [: K7 Y
  33.             $mongoServer .= ':' . $config['port'];
    * l; ]: d9 V+ E; Y& N9 s% ?
  34.         }
    " a1 k: V; S! U4 ?" v
  35.         $mongoServer .= '/' . $config['database'];" w7 w# J: }% ~! {0 w( L/ P9 e

  36. , Z( M! Z( u( S# H
  37.         $this->mongodb = new Manager($mongoServer);
    : c- t3 F4 K: N
  38.         $this->database = $config['database'];
    2 o3 k' W( G! S, @" |9 K+ m
  39.         $this->collection = $config['collection'];
    / I2 K' R7 F& m' |! D
  40.         $this->bulk = new BulkWrite();3 h4 |# @0 i; @: }" O
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    " W/ _$ Y  y- }
  42.     }5 h* d9 G7 a5 i& ^) u9 i1 u% j/ e
  43.   R3 ?! j2 L( O! L/ b
  44.     public function query($where = [], $option = []) {
    ( O9 j) N1 C' Y  B- d2 g& h6 o
  45.         $query = new Query($where, $option);9 m$ r6 o% ~/ y9 T
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    7 C6 n5 }" f. H7 ~) |% _

  47. ) A1 C( ~, ]* a% N3 t
  48.         return json_encode($result);+ a3 ]7 L( q: h+ ]5 v
  49.     }1 l7 e4 k* x7 T8 l8 G* w; Y! _7 W
  50. 7 V6 U& S' e) a# U
  51.     public function count($where = []) {0 @+ x) U1 i- U4 ~4 ^* M6 `, T
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);0 q3 K5 F9 B2 j* T' N3 i& J1 `! Z
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    5 A) I, {8 i: S9 z- n$ z
  54.         $res = $result->toArray();
    % g! N; v% R! M2 \. F4 o; S
  55.         $count = 0;+ K; N; H" r' R& Q+ A6 ?2 r
  56.         if ($res) {
    0 G8 W2 z) a, u1 `4 o! ~8 g' J
  57.             $count = $res[0]->n;
      w% k; x! {0 f. g5 M
  58.         }4 _* w+ a8 N  l! g

  59. / _" |, g3 G& T& S8 P& n
  60.         return $count;3 _+ y" k2 ], J2 M
  61.     }+ y& y; K3 s2 {$ k2 M5 w( S

  62. . x5 x+ D4 X6 D6 @6 m
  63.     public function update($where = [], $update = [], $upsert = false) {, L4 d( E4 R) C) s- q2 e
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    9 r/ B$ l5 q( V: `7 ~  x6 W
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    + j5 A# M! }& R; q" F: Y  n
  66. * Y) C; h- L' l/ O/ j  q
  67.         return $result->getModifiedCount();9 Y0 r. f) I' a/ q! l6 ]4 f& K7 ^
  68.     }
    + x" B; ^- l) z0 |* N4 M
  69. ! }& F6 |5 ~2 _
  70.     public function insert($data = []) {
    1 s) ?$ z! O, M: h8 e; J
  71.         $this->bulk->insert($data);
    5 ~+ w- W1 c7 L/ r, \" t
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);% o* i; ]3 Z1 }( j9 w+ V) |; B
  73. " Z  K# r: Z8 D# [: ^
  74.         return $result->getInsertedCount();
    9 {4 \) s5 _3 L
  75.     }
    " p" k9 T7 K! N) B6 O1 Q; M8 d

  76. + ^& P( d' w: Z; G* j/ C
  77.     public function delete($where = [], $limit = 1) {" y& m" D  Y! M
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    5 n# E, O& Z3 [* i0 r8 _1 q: ^' B
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);5 s; L, S3 f  Z1 m9 y* N

  80. , Y% a  n  I0 [- Z: `
  81.         return $result->getDeletedCount();; N' f' n# n  p. Z% S' p& a/ b
  82.     }" _5 G( H+ ~, y9 Z5 }& R7 T' P9 J$ p
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接

  • . b: t* x" I- U
  1. new MongoClient();
复制代码

  • - a5 H' r7 }; p* n% U1 W2 c
  1. new MongoDB\Client();
复制代码
2.新增
  • 5 @0 z9 x# R) ^) F
  1. $collention->insert($array, $options);
复制代码

  • . U0 D) U" }: Q( Y: r
  1. $resultOne = $collention->insertOne($array, $options);//单
    - t( j; a$ W: R; B& i* {. A
  2. $lastId = $resultOne->getInsertedId();6 ?" h0 ^; l7 K. Y$ I  m" k
  3. $resultMany = $collention->insertMany($array, $options);//多
    % Q7 c$ s. u! t+ a$ _( o2 L: ~. r
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 2 \& C, q0 B5 g" O& Z& q" A
  1. $collention->update($condition, [
    2 n, p( ]0 H0 F) Z6 G
  2.     '$set' => $values
    4 k9 f' n/ V0 J& U) U* f9 X- g7 q& }' l0 T
  3. ,[
    2 \3 e4 o. V' o5 t( W
  4.     'multiple' => true//多条,单条false; t' g" q" N+ g' F5 U. `+ r$ F* N
  5. ]);
复制代码
  • 4 q( S1 ~7 l. R2 Y6 Z0 a4 |
  1. $collection->updateOne(
    ; z0 B( k# x4 }9 G
  2.     ['state' => 'ny'],( e: h$ H0 ^6 @1 V
  3.     ['$set' => ['country' => 'us']]
    ! V$ L7 l5 O- S" T6 F: M! J
  4. );
    . N9 @. |% q( V8 j( t8 I
  5. $updateResult = $collection->updateMany(
    2 s; g2 q: s2 z
  6.     ['state' => 'ny'],& ^5 g; W! ]3 v5 H
  7.     ['$set' => ['country' => 'us']]7 g# d! H, ]5 o( v
  8. );6 Y+ A; f$ m/ F' _: D( g
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • ' d/ y  t# P9 c
  1. $cursor = $collection->find($condition, [
    * \4 C* D! m0 C0 T* j9 e
  2.     'name' => true//指定字段5 ~4 K# V' F# |6 B+ Z3 c
  3. ]);
    2 g( M- N7 {6 R2 |+ v2 L! h: ^$ u
  4. $cursor->skip(5);6 d0 h! u0 @3 i9 I
  5. $cursor->limit(5);0 w% o) L/ Z4 v2 B! ?; q% A, ?+ O
  6. $cursor->sort([
    , {9 t/ p" i5 o+ `0 }6 ?6 k+ q
  7.     'time' => -1
    . G* o7 {2 I$ b7 }  g
  8. ]);
复制代码

  • 4 f3 E( s1 b% n1 ?+ ~1 j
  1. $cursor = $collection->find($condition, [3 d- h" T; Z# ~
  2.     'skip' => 5,
    , H1 R, f" w2 d1 d
  3.     'limit' => 5,
    , E+ a5 i& R) V, m& S
  4.     'sort' => [; \  V  `; s! h/ ~# Y  x' `
  5.         'time' => -1
    1 r% P7 I$ C  m9 J5 C
  6.     ],//排序. }. P0 y9 r+ Y$ @/ ]; t! P! u
  7.     'projection' => [
    + p+ F- C" M# e& l) T2 q
  8.         'name' => 1//指定字段
    3 W5 R" P1 x; ~7 i2 ^* C3 x% v* T' G
  9.     ]- @9 Z7 l; d# L+ i
  10. ]);
复制代码
5.删除

  • ' S; R/ X# l  @* f
  1. $collention->remove($condition, [
    , _* D+ O. N* `
  2.     'justOne' => false//删单条6 @7 G9 N% j+ r: y' o- n: a( `
  3. ]);
    4 n3 b) ]: T; n1 x$ x
  4. $collention->remove([]);//删所有
复制代码

  • + [3 A# u( C; O/ ^3 J+ }' I
  1. $result = $collention->deleteOne($condition, $options);
    # w8 m4 i* f! ?8 W- q
  2. $collention->deleteMany($condition, $options);. V' @, z6 V" Z$ V! o8 Q8 x
  3. 8 l, A0 j! L% _+ e7 f1 b
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([6 Z: _5 M8 S, O: m
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    , y* m7 L6 ^9 U
  3. ], [
    5 L4 Z& e/ a5 x1 g, _
  4.     '$inc' => ['id' => 1]//自增4 p+ ?  V8 N2 a' @% w' \
  5. ], [
    + r2 I; u3 ^$ P* R! s8 m! |
  6.     '_id' => 0
    3 A& V, g) W# L
  7. ], [$ h) y4 C0 L: V
  8.     'new' => 1//返回修改后的结果,默认是修改前的2 Z# F. y5 O3 A1 [4 c3 X
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([  N8 b# B& ]% ]* f5 O
  2.     '_id' => $tableName
    * A% F0 T& p( U: Z0 C4 v4 M4 M! e* a
  3. ], [
    : ]- e& F. N- k! P- g
  4.     '$inc' => ['id' => 1]
    * ]" y6 y7 a  x* \! z/ l
  5. ], [$ m. p$ m6 k; w- ~3 T' n
  6.     'projection' => ['id' => 1],
    - j* B% H" l3 ]6 d
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    + }( N* r* \" \' Z# P
  8. ]);
复制代码
6 e% y' b$ ]2 m* \9 j1 w3 L

; Q9 i  @, {1 v: U" \% l6 z  l) A
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:01 , Processed in 0.054284 second(s), 20 queries .

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