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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11733|回复: 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
    2 ]& r' D1 }. e8 Y% S4 Z8 |: r& W
  2. & j0 k# i% t. P5 ]
  3. use MongoDB\Driver\Manager;
    * b( O6 c; R  p; I* _
  4. use MongoDB\Driver\BulkWrite;
    5 L+ D5 R" r: G  I4 y+ a; V; E
  5. use MongoDB\Driver\WriteConcern;
    $ `. G) ]  ]& R( L  Z4 w% V
  6. use MongoDB\Driver\Query;% H+ t. w( F" J1 G& M
  7. use MongoDB\Driver\Command;. a$ o/ d9 X4 A7 p" o1 F3 D3 B0 A
  8. 1 ~( _0 ^& I' M; W- {: N8 ^. `# u, N: S) T
  9. class MongoDb {% I# E* M# y6 p6 n5 q

  10. 2 X% U& g0 \) c
  11.     protected $mongodb;
    ) V  P3 q0 [, c( [- Z/ L
  12.     protected $database;
    # r* y" i; e4 u4 S! L$ n4 x) k0 |
  13.     protected $collection;
    $ g: x  V8 o0 x
  14.     protected $bulk;0 j, q- m& a: C3 @7 q5 M
  15.     protected $writeConcern;7 _/ O0 V' K6 N2 C
  16.     protected $defaultConfig+ Y1 f( d: Z5 @! g$ n
  17.         = [" M1 s4 V1 D% k+ E$ N2 Z8 k+ [7 N
  18.             'hostname' => 'localhost',
    ; I2 Y! V7 T4 e* E( ^  D9 i
  19.             'port' => '27017',
    ( C9 b, J: m6 l9 I& t- K& W
  20.             'username' => '',
    # o6 b1 D8 g. C0 b' l
  21.             'password' => '',: ?+ N: j  a- ?$ W
  22.             'database' => 'test'
    , S5 t& B, \  k3 W3 Y
  23.         ];
    9 Z8 i  E2 ], D. A$ ?& E7 ^! r

  24. - ~3 Q$ k4 j! c/ ], G
  25.     public function __construct($config) {
    , [! [5 G( ^3 {/ o
  26.         $config = array_merge($this->defaultConfig, $config);) @7 \' l& ?5 A. u6 x* P6 f
  27.         $mongoServer = "mongodb://";
    : J9 {6 q/ o( W+ B( g1 `
  28.         if ($config['username']) {4 r6 B) i; v3 ]3 V
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    1 G# ~/ ^3 E1 y( H$ L1 d4 t8 F8 s
  30.         }
    " L: [  ]& g! l% c
  31.         $mongoServer .= $config['hostname'];5 p+ R# D4 a$ q/ y) t
  32.         if ($config['port']) {
      z' D, l+ y0 E  F
  33.             $mongoServer .= ':' . $config['port'];, [  d$ [. [; Y, @1 R6 R4 ~
  34.         }( o+ g+ X6 N) W/ g4 R
  35.         $mongoServer .= '/' . $config['database'];! b# k* U; J) g( t5 x

  36. & S- J4 f: E  A" A; I% Z
  37.         $this->mongodb = new Manager($mongoServer);
    2 M5 B9 ?: `8 t7 n! G! }
  38.         $this->database = $config['database'];
    8 |/ I2 j; M5 B9 z; R- L+ X
  39.         $this->collection = $config['collection'];
    * ?9 H5 l5 J; f/ p& i
  40.         $this->bulk = new BulkWrite();
    ! p2 ^/ C! r) i2 L
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    / s. O$ d) k/ o
  42.     }
    / `4 k; C8 k" b) ~; X1 S

  43. $ n; H& q4 @# @/ y- i
  44.     public function query($where = [], $option = []) {
    + f1 v' h6 \' ?0 ^& `1 M' t
  45.         $query = new Query($where, $option);! N, J- D5 c; b: D# I
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    , j  d7 G$ J" L6 \+ L9 f) f  ~6 R

  47. 5 V7 u: n$ Q! P6 `, e+ h
  48.         return json_encode($result);* E% N. j6 _0 ^# {- X
  49.     }& O% U) }% T( Q$ X

  50. ) a1 D+ I/ m+ F4 C; R- }& \
  51.     public function count($where = []) {
    ( C% U$ {1 _) n" l2 r+ J5 X' T
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);9 p/ U7 e7 k8 c& _$ y, B# v
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    , Z0 j. _8 E* i
  54.         $res = $result->toArray();
    7 @4 |* Z5 w6 ~* n; r. {% m2 e  R
  55.         $count = 0;
    ! Y& w, S* c0 N4 u; [# w5 ~
  56.         if ($res) {
    4 M8 c& l5 {/ X6 |. N! W, {
  57.             $count = $res[0]->n;
    : P+ ~, n6 w9 U( X" t) {( [
  58.         }
    8 P$ W4 \) h7 f
  59. + b/ P0 p3 L% w: S
  60.         return $count;
    # A: P! A3 f4 L. T1 \
  61.     }) ~. J  \' B+ D1 w

  62. / _4 S' {1 p  k4 N6 b# W$ p
  63.     public function update($where = [], $update = [], $upsert = false) {
    ( T8 g+ M' ?( U- s; E( S0 S# V
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);' Q  c: [. |' R. A1 Y' J4 E" }
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);' t# ?( ]9 Y- E# P8 [2 z

  66. 9 u5 O/ c4 u' X9 t- H3 L! }
  67.         return $result->getModifiedCount();
    7 P* w# X. v! x: U
  68.     }7 j5 [+ b9 F5 G2 `
  69. 0 Q( h7 u( N8 A
  70.     public function insert($data = []) {0 z" A* Q  v. x
  71.         $this->bulk->insert($data);! z3 P+ o  n0 y+ @' w
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
      \2 h3 E) P3 W5 e# B  h# b) W! O
  73.   d9 Y$ e5 K5 f* B7 Q
  74.         return $result->getInsertedCount();. }5 i: ]1 k* G9 z% U
  75.     }  Q. m9 z! o9 u/ U5 B# O

  76. 0 o1 U0 I  t7 k6 i. R, U
  77.     public function delete($where = [], $limit = 1) {* `  `! _& d9 F* v/ p, O0 @' q- v
  78.         $this->bulk->delete($where, ['limit' => $limit]);/ X5 |, d! y3 z2 @$ y
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);0 e1 n# L% r, z! c  Z( I
  80. ( Y" j2 y2 t" ]4 N
  81.         return $result->getDeletedCount();8 Y: [, P1 j! b
  82.     }0 }5 {' l. I& s* X9 |
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • * l4 F& |* [  z# U
  1. new MongoClient();
复制代码
  • + N1 |9 f" E3 }* d
  1. new MongoDB\Client();
复制代码
2.新增

  • & D* w) I: Q+ G0 ~( j
  1. $collention->insert($array, $options);
复制代码
  • % W, i# C! z. x& C0 l
  1. $resultOne = $collention->insertOne($array, $options);//单4 g: T# s+ G! X: ^1 A
  2. $lastId = $resultOne->getInsertedId();# e! i3 [4 B  O! J7 h
  3. $resultMany = $collention->insertMany($array, $options);//多6 {+ c2 p% v" _. S1 W2 X  B( d
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • 2 W. M! x# g! o+ L8 y2 u) V( m8 o7 M
  1. $collention->update($condition, [2 F* B2 P. a' t% r( ^. ?
  2.     '$set' => $values/ B4 H0 f$ i- f+ t' }
  3. ,[
    ( P5 w; F4 F4 s& M
  4.     'multiple' => true//多条,单条false
    % v2 W. l5 }& ?4 i& O4 \& P& y, Z
  5. ]);
复制代码
  • ! j: G. V4 r( R- ~" p
  1. $collection->updateOne(
    " E8 k2 q, [8 \+ O6 @
  2.     ['state' => 'ny'],& \! ]9 M, _# }# z" t) E
  3.     ['$set' => ['country' => 'us']]
    # `! b: x( b& H4 b
  4. );/ M$ W$ F3 o0 |; Z. Z
  5. $updateResult = $collection->updateMany(
    . k6 _) F% x! L
  6.     ['state' => 'ny'],
    : {( g# ]1 n; A4 z) t# o; C
  7.     ['$set' => ['country' => 'us']]
    . r* u$ C: s  o( V
  8. );
    / K. F; |4 H9 q$ P8 u
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • " C9 O/ g- x* a2 T$ s
  1. $cursor = $collection->find($condition, [  X* Y( x! L  w7 d, s
  2.     'name' => true//指定字段+ X1 _/ i( W9 y8 a( e3 p# D# |
  3. ]);% l! i5 y/ v! ?# G  Y% k
  4. $cursor->skip(5);
    0 b7 w4 l2 T- |' u1 P+ w
  5. $cursor->limit(5);
    3 A! O, c9 f9 Y: w$ k
  6. $cursor->sort([0 H* F5 @( t: [' ~
  7.     'time' => -1
    ( I+ O9 K. Y$ b. `" u% j( K. X
  8. ]);
复制代码
  • / q. H% R% m& k7 ?+ S
  1. $cursor = $collection->find($condition, [
    9 k7 h+ \8 Z* e9 t6 f
  2.     'skip' => 5,( f+ _) h8 q" ?" h5 L+ @
  3.     'limit' => 5,
    * ^! ?7 t9 p3 n
  4.     'sort' => [0 P3 N1 @- P; w* P- Z9 `; J8 Q
  5.         'time' => -16 r6 n3 U  y7 m3 D5 ~. U
  6.     ],//排序5 _  M0 n4 C# S, P! H7 j) o: S9 C
  7.     'projection' => [7 O8 W& z, g# L/ t0 J. F) r/ |3 e
  8.         'name' => 1//指定字段
    7 U2 w/ I/ T* v. S; |
  9.     ]) `' F4 Y6 K0 X3 f, y) G
  10. ]);
复制代码
5.删除
  • . c0 \( W" e! J6 |; l0 ^" z
  1. $collention->remove($condition, [! ?6 ]! c3 f( Z/ G; `
  2.     'justOne' => false//删单条. L$ U& S' Y' E; Q  T
  3. ]);
    6 S2 G& }9 u. D! S8 P: }) {
  4. $collention->remove([]);//删所有
复制代码

  • 5 p/ ?& E( S& {2 A% U1 L5 Q
  1. $result = $collention->deleteOne($condition, $options);
    # x) P' `5 G  K  N4 e& B
  2. $collention->deleteMany($condition, $options);
    . W* p: Q& a2 L/ O
  3. ; V  S9 H8 Y) M
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    $ p5 r# j" Z/ x. t9 q
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    ( X) u1 d2 O/ S7 {6 L
  3. ], [6 s" w9 S, m# }* @3 _. W: o+ T
  4.     '$inc' => ['id' => 1]//自增
    5 }0 P" e+ E. T. t
  5. ], [
    5 i! s2 K* s5 i
  6.     '_id' => 0
    . ]2 S5 u; q8 N0 ^3 C+ S  l
  7. ], [" G- U; A/ n* O+ H
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    5 b  {1 u. i# t% v2 f3 x8 N5 O
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([- E, a9 A5 S0 l! ^( N+ L
  2.     '_id' => $tableName4 `+ v9 U2 W: e. J. P1 ]/ u
  3. ], [: u5 |7 B2 ^2 C; Z
  4.     '$inc' => ['id' => 1]/ Y7 h# S: w; ^* U/ a3 L  K
  5. ], [
      B' A0 `  D* T7 K# m
  6.     'projection' => ['id' => 1],
    ! G; m  z0 D" Y. [' \% R  T/ D
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER8 r' p( d- `  Z
  8. ]);
复制代码

/ V6 j/ O4 H' S/ o* K1 {) @, g& B$ _! k  n, @7 @- X9 u3 X9 d
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-6-1 14:54 , Processed in 0.134517 second(s), 19 queries .

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