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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16098|回复: 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) j1 ]1 l4 _$ Y# i" L

  2. 3 \% P' T1 L* J+ F
  3. use MongoDB\Driver\Manager;. [* F2 B  A8 V" a
  4. use MongoDB\Driver\BulkWrite;0 f  y, ]& j( x8 Z5 g. h. l8 _
  5. use MongoDB\Driver\WriteConcern;
    & v( K( W% v; m* T2 r! O, J. c
  6. use MongoDB\Driver\Query;6 D/ n2 {: E4 }8 }, [6 G: C! E
  7. use MongoDB\Driver\Command;
    $ n, c1 E9 @6 b7 u4 s. V4 u

  8. % R% Q8 g, x; s: ^$ |( U+ f
  9. class MongoDb {6 q; }7 X& l9 S- l

  10. 4 i% S* H: [, S) C
  11.     protected $mongodb;
    2 S3 t9 i$ w) t) d
  12.     protected $database;' G$ Z  \/ [% k) {3 I: n4 n5 I
  13.     protected $collection;  b3 X" V( ~  A! a
  14.     protected $bulk;; m6 p1 M: ~6 M/ x" l  `9 a
  15.     protected $writeConcern;
    ( H7 S  t- n6 N4 Z& W7 @: E
  16.     protected $defaultConfig8 X! g" G4 E- S. f. u
  17.         = [2 u# S. U: }, c
  18.             'hostname' => 'localhost',
    8 x- ]1 P/ e9 j0 e2 @  q& g
  19.             'port' => '27017',; h. F& }8 e8 s# n2 C
  20.             'username' => '',
    $ k+ I- j" I0 }# T. {6 P+ h
  21.             'password' => '',
    ' j% K4 L- h  z$ q
  22.             'database' => 'test'
    7 d" C( F. L% u* T6 {8 K
  23.         ];) ?6 a- h# |  ]( m. t4 o, v
  24. 2 v2 O4 |7 n+ u- b- z5 ]' v
  25.     public function __construct($config) {. J! ^$ F3 K" f4 V1 U
  26.         $config = array_merge($this->defaultConfig, $config);
    ! Y$ x4 a) b- q( S( n1 C) B
  27.         $mongoServer = "mongodb://";/ L1 [. q% o: C+ X! u+ w
  28.         if ($config['username']) {5 Y( s# `* E. h. h/ I3 @
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';7 F8 ]. D* f+ `2 r) D" W
  30.         }
    4 y% {- ?9 @9 H/ Q6 ]$ b  `* J
  31.         $mongoServer .= $config['hostname'];% @' c# ]4 {4 G9 A# a) v, L
  32.         if ($config['port']) {9 j' t: ?! J" ]" E1 Q; _
  33.             $mongoServer .= ':' . $config['port'];+ n: C0 \$ _; G& D
  34.         }6 c+ p7 e& ^* t# v% K6 o9 S
  35.         $mongoServer .= '/' . $config['database'];
    4 U+ j. i9 z% G) k1 d* D

  36. * y+ w# W9 Q' c6 @2 \; @9 @" p- P! P
  37.         $this->mongodb = new Manager($mongoServer);
    # L! I2 }8 o" F1 A
  38.         $this->database = $config['database'];1 U8 Q- F" c9 f6 y3 }
  39.         $this->collection = $config['collection'];6 h# K8 ^& q6 U8 _6 {
  40.         $this->bulk = new BulkWrite();
    . D% E% [% Q4 L( h" c$ y; @9 t
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    # y  D' s/ z* `
  42.     }" `' P' Z; N0 z3 K! p# Z0 g, m
  43. 9 V+ c% D; i" ]& Q
  44.     public function query($where = [], $option = []) {+ V9 Z/ \  Z, X, o
  45.         $query = new Query($where, $option);
    4 V0 Z- N! X" B$ F0 R6 r' J" x0 b
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    ; @0 F: |% U9 U6 E6 B9 u2 u
  47. / f; P  b. M/ I& k. a" B5 E
  48.         return json_encode($result);/ ~0 F3 Z; c( {3 O6 u
  49.     }4 p3 q* r- a( j: c# k
  50. ; j0 P" T! a4 z( b) m
  51.     public function count($where = []) {
    9 V- k+ h$ C4 ]4 j: y4 o
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);9 N* t0 ]! I+ u( p7 ]5 G" ~0 e
  53.         $result = $this->mongodb->executeCommand($this->database, $command);( ~% B4 T, _: N, L/ o" j4 p
  54.         $res = $result->toArray();; G7 Z* x; Y7 Z" s$ {5 J3 g
  55.         $count = 0;6 B7 J; k) [7 L# W' u  E/ V9 e) m: D# m
  56.         if ($res) {( F3 [5 n+ K: g- T; Y
  57.             $count = $res[0]->n;
    * h, [% _( o# K7 |0 }" }& _: f
  58.         }/ @3 }, N) p2 Y( k( B

  59. - P. I; ?6 _2 ~  [; M
  60.         return $count;
    9 C+ C. ^4 c7 x. b% G
  61.     }6 }- X5 G! F! X. `0 G5 ?5 c

  62. " U# c% H; A5 j6 @2 D6 T. @; O' V+ r+ X- I
  63.     public function update($where = [], $update = [], $upsert = false) {
    6 }6 n" W! D  t8 ]
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);% n0 Z9 {9 r# o9 k. L9 t
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    9 V( I* j, x4 H  ]' a( j" Z
  66. , c( y4 M4 J4 e( C& g
  67.         return $result->getModifiedCount();
    9 F: d3 l) s/ z) R# |5 C: T* t. S
  68.     }+ C3 r) T/ U. o2 p$ e
  69. ( g# z3 S% p. o5 }/ O: Z
  70.     public function insert($data = []) {
    7 z0 L" Z# ?: C' B5 r$ l, |6 p6 b, @
  71.         $this->bulk->insert($data);7 T6 n# ?5 e( o/ L' r. X( z
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);) n/ w$ t4 c1 u# \4 Y1 X' W% D
  73. 8 u$ c; t% x% M- ~! S# p* C  p
  74.         return $result->getInsertedCount();: I; c3 a; u7 \+ B7 P: W/ t0 c2 G
  75.     }8 D6 W: D; |* ?  k9 v

  76. : W% p: @+ N8 _" k# Q% j
  77.     public function delete($where = [], $limit = 1) {
    3 Q% `! ]2 G; ~. [7 b! S2 e  k9 l
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    1 y' T, `- _. |
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);9 ^  {4 p  m# y3 o
  80. 8 j$ d2 \" L1 }& E" @, \
  81.         return $result->getDeletedCount();
    ; X& B5 l! o5 q7 G1 ~- ]5 t* U3 @
  82.     }* M7 f: F+ }; g% Y, ]
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接

  • # n  m/ P  h8 N3 K
  1. new MongoClient();
复制代码

  • , d( S  |) E( Z0 M1 b- W% s
  1. new MongoDB\Client();
复制代码
2.新增

  • + z; J5 D  z# I7 }
  1. $collention->insert($array, $options);
复制代码

  • % t8 d' R( b! A/ T" E6 {: U* H' X
  1. $resultOne = $collention->insertOne($array, $options);//单
    4 Z& N3 w7 D1 C* r
  2. $lastId = $resultOne->getInsertedId();
    & B- O  Q. b# X4 Z( J9 j
  3. $resultMany = $collention->insertMany($array, $options);//多7 A( a' I  w3 E, _% |
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改

  • / X+ I, o! D( ?
  1. $collention->update($condition, [& t/ P' B3 u/ X
  2.     '$set' => $values
    9 o7 ]& B0 B: w- [' U
  3. ,[5 H; P$ _0 C" v- Q
  4.     'multiple' => true//多条,单条false; S% Z9 r+ ~/ d& y
  5. ]);
复制代码
  • - N& D3 \9 R! x' p3 m! _
  1. $collection->updateOne(' s) x8 N5 R+ [/ N& M
  2.     ['state' => 'ny'],
    6 R3 o: V9 i& s2 g7 f
  3.     ['$set' => ['country' => 'us']]# n; c6 r. ~) S, Q
  4. );# I" [$ ]$ R; P
  5. $updateResult = $collection->updateMany(/ A* N3 y. x0 [1 r- Q
  6.     ['state' => 'ny'],
    " T6 F" q0 P# g1 ]/ B) ]
  7.     ['$set' => ['country' => 'us']]
    , x0 S0 K' Y: g& j: x& ?# {
  8. );( H  i6 y. e6 [% {: `
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • . C) M) h0 R4 l4 t
  1. $cursor = $collection->find($condition, [8 F3 f6 B; w% q8 E2 ?- Y
  2.     'name' => true//指定字段: n9 e6 N  Z. Z7 u5 w$ x
  3. ]);( [+ H+ n" c: G/ W: }& B: B
  4. $cursor->skip(5);7 F: o( K1 @5 B$ M" R: q. W! [
  5. $cursor->limit(5);& ?, R) d  O0 f, J; o" E% p, a
  6. $cursor->sort([
    5 O2 Q+ z3 d. ~% t$ @' q* y
  7.     'time' => -1, P( j( i* @3 f" w" d
  8. ]);
复制代码
  • # R  I$ @  s* v
  1. $cursor = $collection->find($condition, [" B: k! n' s* W% n7 p
  2.     'skip' => 5,. a9 v5 ^& }- A5 v
  3.     'limit' => 5,( O& _8 z- m: _3 ]. Y5 ?
  4.     'sort' => [
    5 |7 J! g. c; n9 n
  5.         'time' => -1
    1 B  b& ^: k4 D) O- _$ h$ C
  6.     ],//排序  Q6 ]* g: X- T2 d# ?
  7.     'projection' => [
    6 v1 O8 D( W% o, K
  8.         'name' => 1//指定字段
    % |! ], X  D) U! u
  9.     ]# ?$ u7 s8 V5 @- x" ?, P& c
  10. ]);
复制代码
5.删除

  • + v3 y# I+ Q: E+ @& y
  1. $collention->remove($condition, [5 T5 N/ F" u/ R# W" \2 X, ^
  2.     'justOne' => false//删单条
    $ w% k. R' s+ u, S. ]( k
  3. ]);8 L: Y. z% u8 X+ @: Y2 p, t
  4. $collention->remove([]);//删所有
复制代码

  •   N/ `0 |: x7 H( e" }
  1. $result = $collention->deleteOne($condition, $options);  B+ y$ \0 a1 g$ f9 I3 l
  2. $collention->deleteMany($condition, $options);: d9 O# y  b7 ~8 r+ C& T
  3. 2 {- G( b* p0 N7 I: |
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    + f9 t4 X0 A8 U' U8 }* C
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键& T/ k' V( t$ Q  S+ f; z
  3. ], [( [; l1 h3 ~& X
  4.     '$inc' => ['id' => 1]//自增
    $ r, d/ w2 ~, P% F% ?
  5. ], [; T3 @8 ^. [' }- Z' x
  6.     '_id' => 08 m) G( y# J' @2 y4 `
  7. ], [
    1 p4 m$ N+ i  A# w$ p% |
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    % y: U9 O$ M0 c2 J3 F, S4 d* ^% N
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([& j+ H+ n5 Q5 w
  2.     '_id' => $tableName5 y6 e0 ^. s) i0 }- L6 ^
  3. ], [
    / m/ U5 U: ~% b, H) G
  4.     '$inc' => ['id' => 1]
    ' }+ L) y" k* b
  5. ], [
    2 Z1 w- i' }6 j( r9 D, M" T' Y1 l6 Y
  6.     'projection' => ['id' => 1],
    7 ]3 }. S% K8 k" K
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER4 n1 A3 _% K9 f8 Z" G" L' C
  8. ]);
复制代码
: ?) q& R7 T: H7 g( u+ \8 e' @) h
5 z7 X$ w% ^+ x9 X. m  T
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 18:22 , Processed in 0.051324 second(s), 19 queries .

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