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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16084|回复: 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( B7 i6 W5 y6 F! W" ?+ O2 L

  2. 3 v4 @; z8 _) ]" H( ?! H
  3. use MongoDB\Driver\Manager;* [/ B5 C" ]! e$ P
  4. use MongoDB\Driver\BulkWrite;9 u3 E* F5 R4 g; F) G- V
  5. use MongoDB\Driver\WriteConcern;
    . G1 r' q% z) d8 i) K- K
  6. use MongoDB\Driver\Query;! F% q+ f; b0 f2 F2 p3 |# |3 @
  7. use MongoDB\Driver\Command;: ^5 L; X7 h* {  M) c2 v: f

  8. , i% h+ j0 {  t; e( l# }8 [- p5 i
  9. class MongoDb {
    ; _7 ~% u8 B) G0 a+ g9 j( X! W' H3 w

  10. . K+ x/ E, |  M
  11.     protected $mongodb;8 I' {, w4 A) v0 }: S. ~
  12.     protected $database;
    1 U7 _6 i3 A8 E+ S) C$ J# W, J* \
  13.     protected $collection;4 K  Q$ Q1 |0 v% J6 W/ Z
  14.     protected $bulk;7 Y% D3 F' Z' @) g" Y( o
  15.     protected $writeConcern;
    0 u4 J9 f* g  t, x5 C8 N& k) T: Z
  16.     protected $defaultConfig
    2 W, d5 j, i8 e8 g
  17.         = [
    / f. X8 w' d5 z* v0 M0 I7 K, b
  18.             'hostname' => 'localhost',: d# ~! r2 J) q- f# w$ b5 n
  19.             'port' => '27017',+ b: t% @/ g5 y) W' l/ X
  20.             'username' => '',2 `% }% N: `! b0 U
  21.             'password' => '',1 ^, I1 ?, u' l8 r' M  @0 n  Q3 [! j) L; F
  22.             'database' => 'test'7 n6 E1 \9 q: s2 U1 P
  23.         ];
    1 R- o; [1 b& A4 f" m

  24. * @8 I8 s' Y( }1 x1 f6 e
  25.     public function __construct($config) {5 ^8 I% r* x1 a0 o: J" q% @
  26.         $config = array_merge($this->defaultConfig, $config);8 n; t! D" P5 F2 }9 ^7 U; H  N+ {
  27.         $mongoServer = "mongodb://";
    $ n2 `. s/ T8 A. U. C
  28.         if ($config['username']) {1 e  n  j3 q& l/ M0 [3 o
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';5 }. t% A% ]9 F2 G3 Y8 ?) x* l$ b
  30.         }" @: f2 S* I2 l, W
  31.         $mongoServer .= $config['hostname'];
    * B1 _  Z6 q% @( y, ~* c
  32.         if ($config['port']) {3 k+ d( M, M5 u: F: A# d
  33.             $mongoServer .= ':' . $config['port'];
    * s# [  I% d; @) j7 ~
  34.         }
    + y. ^7 ^2 j! H; H; `# M& B; `
  35.         $mongoServer .= '/' . $config['database'];9 ]- F1 n9 [2 U, u8 Q, v

  36. ; Z) Y! v$ F1 x+ ]- n$ G
  37.         $this->mongodb = new Manager($mongoServer);
    & W4 W- ^* J; ^) L' b, `% N' k
  38.         $this->database = $config['database'];
    + i( s, V0 y# I9 u" m8 w
  39.         $this->collection = $config['collection'];3 c! w$ O0 G: J, a' g
  40.         $this->bulk = new BulkWrite();
    0 h& Y" z) y+ E2 h; c
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    0 |8 k; X6 o+ J4 C
  42.     }# K& p' o2 z3 \4 f0 M* }7 @" @
  43. 2 w0 \9 `3 w) B' a$ I# R! y
  44.     public function query($where = [], $option = []) {
    + A$ }# F" G5 L( c
  45.         $query = new Query($where, $option);  O$ j" m, H9 i( X. F( r9 L4 F
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    . Q' o  b1 \( `6 @

  47. % w1 t/ Q7 U: w: O4 g  J. N
  48.         return json_encode($result);, Z$ j" @8 l) Y- G9 V5 M( _
  49.     }
    " R6 p' [& ~9 |0 U# Y$ I1 i
  50. 3 y! W6 O+ C$ L. p# |  c& F
  51.     public function count($where = []) {
    1 ?9 [/ c- c: y( r1 c; i
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);
    ) x9 F* R' }! e# v
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    0 o8 a- y3 U) |& |+ K/ b
  54.         $res = $result->toArray();* x; v% g+ W. a
  55.         $count = 0;
    ! y& o0 J. @4 |  G* ~6 T2 B+ C
  56.         if ($res) {
    2 w" U4 H% ]. u2 L% c! f6 F/ ~6 P
  57.             $count = $res[0]->n;# W2 @* T7 O# v3 f2 O# y
  58.         }
    + g) N. ^# {) d0 P4 W
  59. / h- r9 \$ ]+ Q# Q/ I" F
  60.         return $count;# \- Q9 s: ?5 a
  61.     }
    - o' Q" _, v- n8 Z
  62. 2 `$ W8 {; s; G0 u0 N
  63.     public function update($where = [], $update = [], $upsert = false) {4 H! r: \& F( E& }+ i8 K/ z/ _
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    * `7 f, G* c7 H; U
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ; |$ G3 m8 I4 Y2 a9 S/ j& T+ i; C7 o
  66. 2 F) k: w, ?7 G1 [- f8 J
  67.         return $result->getModifiedCount();
    8 V$ E# |3 n( I6 b0 l
  68.     }
    $ t9 p$ Q/ ~$ ~, o3 }  ^$ h2 m
  69. ! @' j% g- L4 s. z
  70.     public function insert($data = []) {" O1 y$ d9 i1 L5 l+ G1 G5 P3 j
  71.         $this->bulk->insert($data);
    ( B6 |" S% D3 p5 ?: u6 @/ L. {
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    3 ^6 y" `! O5 m
  73. ) z8 L- d' Z  P3 i" ~& V5 _
  74.         return $result->getInsertedCount();! H+ ~, D3 ?& Y( e& \' M, v
  75.     }2 g+ e$ c* k( x" R: D

  76. : A  ]& \3 G9 u/ E6 c
  77.     public function delete($where = [], $limit = 1) {
    7 ]4 [. K0 Z' Z- {, x
  78.         $this->bulk->delete($where, ['limit' => $limit]);6 |, A5 q3 N6 Z) L7 ?- E/ {
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);  i' L5 T. z3 Y! W
  80. : g5 H& W1 n1 w! C5 R: ^
  81.         return $result->getDeletedCount();& r  p! G9 O8 I' M  @
  82.     }+ ?5 `/ y; Z. s
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • 3 @# S/ ]- ]9 a0 e
  1. new MongoClient();
复制代码
  • 0 `& L  B0 f& D0 v
  1. new MongoDB\Client();
复制代码
2.新增

  • 4 G4 P: a" n" \3 M' s1 x
  1. $collention->insert($array, $options);
复制代码

  • ' I8 w  ]# j) I4 u6 V  }
  1. $resultOne = $collention->insertOne($array, $options);//单  V( r% E9 N  Y
  2. $lastId = $resultOne->getInsertedId();0 j5 {9 j+ f! T2 d. E5 }% e
  3. $resultMany = $collention->insertMany($array, $options);//多5 Y4 o- c6 v8 R
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 6 T. u6 N7 j1 b$ j5 @, f3 y
  1. $collention->update($condition, [
    ! b' F9 }8 A0 ~9 Q+ O/ B
  2.     '$set' => $values
    - H9 E- A/ f. P5 \/ N
  3. ,[9 S# V' C2 v( b, |' J* q# l
  4.     'multiple' => true//多条,单条false' G: @' w& u6 u0 u. r
  5. ]);
复制代码

  • 9 A( F4 j- P8 r1 z  \
  1. $collection->updateOne(' l0 R' |; u$ f9 d0 k. b
  2.     ['state' => 'ny'],
    & R7 l6 |9 `8 A# g% m
  3.     ['$set' => ['country' => 'us']]% S+ `+ ?/ Y# i: O9 A) ~6 `$ k
  4. );# i$ s& `% ?7 B+ i
  5. $updateResult = $collection->updateMany(0 ?# m4 H5 T# u9 ~( M
  6.     ['state' => 'ny'],
    3 r7 g) L* }. x3 C  c, G5 l" i) H
  7.     ['$set' => ['country' => 'us']]
    5 _% J* S8 f% c4 v' P
  8. );
    # t- {& t( ^; r' M' y4 K
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • " V( {7 ]. w$ Q2 {
  1. $cursor = $collection->find($condition, [3 U: b9 l- k" p
  2.     'name' => true//指定字段
    6 D. Y0 F5 F) {0 r
  3. ]);! K9 m9 c/ k- ]. S9 ~
  4. $cursor->skip(5);
    . I, Q0 t5 e+ c
  5. $cursor->limit(5);! n# y0 M8 Y8 P" [- D
  6. $cursor->sort([1 @5 k" k. ?+ p
  7.     'time' => -1: N# X; r3 C4 Z* w/ |# i, C5 X" U
  8. ]);
复制代码

  • , B0 ^) d. U% n6 a& J' W7 D. Q( L
  1. $cursor = $collection->find($condition, [0 j1 J7 i3 h9 e$ u" D
  2.     'skip' => 5,
    3 A- @5 m4 S  l8 Q( J7 t
  3.     'limit' => 5,8 M2 E. x1 ~& J9 N3 A/ K! R: X7 ]
  4.     'sort' => [- Y/ D! n+ f! ^  X& }
  5.         'time' => -1+ B; Y8 z% n9 u
  6.     ],//排序, Q0 i1 m0 \9 K5 s/ ]: a
  7.     'projection' => [# H1 E! m2 k( r" g: f
  8.         'name' => 1//指定字段
    9 x" x: `" S! b% I
  9.     ]* L% }  B8 {6 H6 r& B( n
  10. ]);
复制代码
5.删除

  • " ]* l/ h% @% Z
  1. $collention->remove($condition, [6 n: Z% L% ~5 U7 o. `& j+ {3 w
  2.     'justOne' => false//删单条' r* O0 K# g( p; ]! T# T+ r( I- Q
  3. ]);2 N4 W$ T& ^" U2 ~8 U" X
  4. $collention->remove([]);//删所有
复制代码

  • ) i' ?1 J% I* z+ A
  1. $result = $collention->deleteOne($condition, $options);% v- Q; R8 K" g* }' B/ T
  2. $collention->deleteMany($condition, $options);9 O- L8 C1 ~9 `
  3. 1 _$ Y$ X% I& {
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([2 A/ R0 v4 U0 U) U* i
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键6 a1 S8 [- Z' Q* ~4 u1 G. F
  3. ], [
    ( y! b# S! M# p8 ^
  4.     '$inc' => ['id' => 1]//自增: Y( y0 C* S  O
  5. ], [5 B) R2 i/ B& s7 [/ S
  6.     '_id' => 01 F% w# G  N3 O
  7. ], [+ v! B- f; P5 N) n( I5 |
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    % r2 X% d; g1 f( p
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    1 T. z) j3 s7 g6 n6 Y
  2.     '_id' => $tableName
    1 \& n* _7 N& |8 D) \
  3. ], [3 R# }) Y" `! ?* I- _/ v  I
  4.     '$inc' => ['id' => 1]
    , K  [4 k' w; i7 k& l" p7 r
  5. ], [
    # K' h/ p* S- c/ @3 Q3 {# L
  6.     'projection' => ['id' => 1],9 q, @2 _/ W0 |. V; K$ Q
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER! k% A( ?  M- ?' k4 ^8 @
  8. ]);
复制代码
$ Q+ U, d7 ^/ |: _3 o8 m

9 w6 }! W4 ^9 }# P$ W% Q' k, @+ \
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 17:22 , Processed in 0.051640 second(s), 20 queries .

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