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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16544|回复: 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. <?php1 ^4 T* G$ M) c4 z

  2. # @1 |: X" F' H9 W
  3. use MongoDB\Driver\Manager;# r3 l, N$ A3 Y6 R
  4. use MongoDB\Driver\BulkWrite;
    $ b: Z+ a- w' t+ H
  5. use MongoDB\Driver\WriteConcern;# d/ a1 d& I' Q: B$ U* v. @3 ?
  6. use MongoDB\Driver\Query;% n" \: z) j' [
  7. use MongoDB\Driver\Command;( B9 H4 c( `5 O: f# R0 ~
  8. / p0 p# e1 @& T1 t# c* @6 a, r6 l
  9. class MongoDb {' L' w+ |& ~6 x6 A; K. C
  10. % v( X. A& i9 t% ~! `  [* }% g
  11.     protected $mongodb;
    * G7 r( [% \; q8 t% Z5 {: O
  12.     protected $database;; G' i3 _/ e% B  f% m& q  @) d
  13.     protected $collection;; M, v/ U1 {3 a7 S9 W1 S* e: m1 ?
  14.     protected $bulk;
    + g3 g$ w) G& _8 E- P6 F# k
  15.     protected $writeConcern;9 l1 a& c8 `$ }5 i2 I
  16.     protected $defaultConfig7 o' X3 J: v; t1 U  }* [
  17.         = [- t. U( d2 i- T) g% @; |6 s
  18.             'hostname' => 'localhost',6 ~+ w: A! J0 ?0 x2 x% e! s2 j
  19.             'port' => '27017',
    5 k' D+ G6 x" [* m
  20.             'username' => '',
    / C% H( U! ^) y; H: U0 e$ g. D: [
  21.             'password' => '',0 i. y. [% ^% @! _, y/ I  n
  22.             'database' => 'test'/ A& \8 C( d5 B8 }* U
  23.         ];& Q" W3 Y- I4 T
  24. ' r  D' d  a6 x& e4 v8 C
  25.     public function __construct($config) {
    - V# `: \' J! Y0 d
  26.         $config = array_merge($this->defaultConfig, $config);) I% x5 r. Z- q! X, P
  27.         $mongoServer = "mongodb://";) Y* ]# ^$ G; f, _, u
  28.         if ($config['username']) {. p7 V8 ]! [5 \/ v% r
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';$ Y- v+ `! Y, [
  30.         }
    , G$ [+ C; I# F: J# a. y1 j
  31.         $mongoServer .= $config['hostname'];5 I( Y5 Y$ R( n1 y" J  x
  32.         if ($config['port']) {9 q) }5 H+ l+ b, M+ @* P" V
  33.             $mongoServer .= ':' . $config['port'];
    ) S5 x8 @& c' [/ n  f1 o4 ]
  34.         }8 i6 A/ |2 |( p( Y
  35.         $mongoServer .= '/' . $config['database'];
    , G6 ~7 ~5 _5 p/ R$ o# t3 C$ {: k8 y
  36. ; [8 S) o2 j4 g9 g3 f
  37.         $this->mongodb = new Manager($mongoServer);
    / q% x: C8 o* X% G& f- ?
  38.         $this->database = $config['database'];
    2 |4 {+ k. Z# Q2 m+ f1 d. O
  39.         $this->collection = $config['collection'];. V; d* t; A  s
  40.         $this->bulk = new BulkWrite();
    4 j. \3 G- C/ m
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);" k# _) Z! e0 |5 B+ Q
  42.     }" Y$ g6 z* u2 N) t, L. H& x
  43. 9 v8 a. m+ F- k' E* ^
  44.     public function query($where = [], $option = []) {- ]" `0 @1 H2 U4 m( p
  45.         $query = new Query($where, $option);8 {) X3 v4 F: Q& H# m: O+ \" P. H  X
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);% a7 Q4 _5 ?" x7 W9 j

  47. & [2 m8 b! H0 h% Y# `
  48.         return json_encode($result);
    ! p( I" _! f% c2 N' u
  49.     }
    ! i) E( ^$ l; S3 C( I9 X& {, o
  50. ' I$ W! {$ @% x
  51.     public function count($where = []) {! {0 f; x0 t$ H: o+ C0 Y
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);" w5 f% l" H# S/ Y. P
  53.         $result = $this->mongodb->executeCommand($this->database, $command);6 x- N5 G% Q4 C4 {
  54.         $res = $result->toArray();
    8 h; ^; b, g6 r+ `9 w
  55.         $count = 0;
    % u; U: L, w, C! V, M3 s' T7 z
  56.         if ($res) {' d& k# P5 b' l- O/ U" r
  57.             $count = $res[0]->n;
    . f: {, R9 c  Q4 p# `6 N
  58.         }1 G5 k# C3 ]+ B' t3 e; F% n

  59. * L- G: a6 o2 b2 J
  60.         return $count;
      ^$ I' X' U$ R$ P% W
  61.     }
    & Q5 k( Y+ f7 a% M

  62. 5 r; e$ b& f! j+ E  g* x+ E
  63.     public function update($where = [], $update = [], $upsert = false) {
    : z. O" q+ w# v8 Q# v
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    2 j* ]8 d! q* [1 p" J
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);% N; Y" s9 ?+ ]7 U
  66.   x7 x  O  e. K: M4 ~$ D6 w
  67.         return $result->getModifiedCount();
    1 r: b0 O5 ^$ Z
  68.     }
    4 M( K/ A: u8 j4 W& `" m; F) o/ r

  69. 6 S8 W/ d( _  P% E' T5 M1 ~
  70.     public function insert($data = []) {5 P% F8 S  r& e: o( ?$ m5 o
  71.         $this->bulk->insert($data);8 j+ F7 F$ p3 m5 x
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    * c8 |/ a, p  V, P, K0 G3 I

  73. 3 \( o1 _: M' w! f& K1 S
  74.         return $result->getInsertedCount();( D8 z0 M- ~, A) h: J
  75.     }
    & }) |" P5 {' b. P
  76.   t+ x6 F$ h( ]' u2 @! q! X
  77.     public function delete($where = [], $limit = 1) {
    - z: x! H0 Q: C# Q
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    , C  T! l  K; l9 ~, |$ f6 \; h
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    " |& P8 }3 [! z& I- T6 L. y
  80. : T: s! ?6 x' k1 m$ w+ c
  81.         return $result->getDeletedCount();
    % q1 c! @7 p% P% S" x2 I5 p/ X! C
  82.     }( V: ~! g. X6 }. _  Z5 k  m; B5 d! X
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • ( e* ?* o$ h, X! T" [
  1. new MongoClient();
复制代码
  • $ D$ V/ N$ A& ?/ @7 w# |6 g
  1. new MongoDB\Client();
复制代码
2.新增

  • : Y- N5 u  ~$ H& N% |  U
  1. $collention->insert($array, $options);
复制代码
  • 7 y9 B! P) ?4 E" X  Z; r5 f/ n4 z
  1. $resultOne = $collention->insertOne($array, $options);//单, H6 U  y. _" k: V: i, r4 b
  2. $lastId = $resultOne->getInsertedId();
    + v! f2 e% d# k, M
  3. $resultMany = $collention->insertMany($array, $options);//多
    % [) [$ n! I! g8 W5 B
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • & V3 [. }9 V' M4 H5 c: v! O
  1. $collention->update($condition, [
    . n# v0 ^7 Q+ b; v* p* {
  2.     '$set' => $values" x/ `- W* ~& w; l+ W) C' @$ q* c8 ?2 U9 P
  3. ,[
    0 N" _4 k- y! m3 `1 }( F/ ]+ V, [
  4.     'multiple' => true//多条,单条false
    ( \; @3 _  R* l0 H% s6 R9 A$ k
  5. ]);
复制代码
  • 3 {; w% H% t7 F! x1 m5 {6 f
  1. $collection->updateOne(
    , Y# q& e% b+ K9 j# n7 Q
  2.     ['state' => 'ny'],
    & u" }1 e1 s. F% f
  3.     ['$set' => ['country' => 'us']]# [: m$ M* M: ^: Y
  4. );
    * u2 T+ V& S, V% z  [4 I  r8 z2 Z" F7 w
  5. $updateResult = $collection->updateMany(
    1 n; x' a; l9 f+ {! a" G( P
  6.     ['state' => 'ny'],5 E( Q  l' q- ?5 w0 Q9 N
  7.     ['$set' => ['country' => 'us']]
    8 }8 \! @0 J$ O( l6 u- H  S2 G1 r5 q: o
  8. );) `4 z$ L" u* H( v! n" p! [
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • $ M1 Q# h) ?8 b& @: z  j; E1 h
  1. $cursor = $collection->find($condition, [
    8 q/ T: E8 ?6 `3 j
  2.     'name' => true//指定字段4 v3 j6 m, y8 M1 F, L+ ]5 @( f
  3. ]);
    ' b$ g. ~- @( F/ n/ ]" m
  4. $cursor->skip(5);- R8 L# A) G, `$ ~8 ]
  5. $cursor->limit(5);8 q) N) w! E1 w4 H
  6. $cursor->sort([
    # B7 F6 v4 m6 ?* j
  7.     'time' => -15 ]- M: j7 V& d
  8. ]);
复制代码
  • 4 N2 \  H; [! J  z
  1. $cursor = $collection->find($condition, [
    0 b, o% M7 Q# [
  2.     'skip' => 5,; M( y% g( S1 f  p# Z* e
  3.     'limit' => 5,7 G. U! q. O' g- r! n: p
  4.     'sort' => [# e- h4 C% n% a; l8 ^" N
  5.         'time' => -1
    1 J9 J: q3 H0 b, f! Z
  6.     ],//排序
    & N; @6 U+ Z+ s! [
  7.     'projection' => [0 F2 E1 Z4 B* X0 c' r
  8.         'name' => 1//指定字段( X, C8 p: _8 i* F; {
  9.     ]
    5 }# S- c4 [; C  d
  10. ]);
复制代码
5.删除

  • ( _# o4 }/ w6 U- f$ C2 x
  1. $collention->remove($condition, [
    : H# h9 C- S, }* @  E$ n4 L( U
  2.     'justOne' => false//删单条
    " g( D3 h8 Q1 u6 b" H: Z
  3. ]);3 [: g9 h/ c' ~  p2 `9 p
  4. $collention->remove([]);//删所有
复制代码
  • 0 V9 {" H- Z% l6 J% G# G# U; B
  1. $result = $collention->deleteOne($condition, $options);
    ! y! g: Y8 ~: v( C" G
  2. $collention->deleteMany($condition, $options);2 z6 [" ?& `) B) Y" g+ u; c% P
  3. 5 ]; T$ l4 L: K8 h; z3 `2 h
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([8 ~6 J5 ^  Q1 @& T$ G) j9 }8 o9 B
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键$ l0 A( R6 Z: U5 R4 E( c
  3. ], [
    : S, i0 X& w: C, ?
  4.     '$inc' => ['id' => 1]//自增
    ' u8 V) w6 _+ Y
  5. ], [* N) r9 k1 A( l- H5 x
  6.     '_id' => 0
    ' ?) U: \& u/ s
  7. ], [
    / t$ P, [+ |2 H1 y. \+ \
  8.     'new' => 1//返回修改后的结果,默认是修改前的0 S& b# V( a/ r* `+ A* M: a+ P7 |* h% Z2 G
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    . A5 v7 z. K3 G
  2.     '_id' => $tableName
    , x" V- F; O3 j; O
  3. ], [
    2 N  }" M! l$ ~/ q7 q% N, j2 F# |
  4.     '$inc' => ['id' => 1]
    ! s1 C  t- l. i1 Z& j
  5. ], [
    + A: N7 v8 i: a8 M- ?0 @4 C
  6.     'projection' => ['id' => 1],$ N+ h& U* C' ]" ^
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    # R, \$ [7 L+ T2 ^
  8. ]);
复制代码
0 S3 G7 x6 a. E, R% I: z" X7 O' ^

0 Z9 p4 w  @- ]$ _
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 01:21 , Processed in 0.058236 second(s), 19 queries .

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