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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 17004|回复: 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. <?php2 q# o; \& d6 g$ n7 Q7 |# W7 g6 f
  2. 5 t- _9 x# K( a9 m+ s1 k5 ^
  3. use MongoDB\Driver\Manager;( k) ?0 S$ I: S, x; ^
  4. use MongoDB\Driver\BulkWrite;
    ; R0 ~: D8 {9 f' {' G. B$ A
  5. use MongoDB\Driver\WriteConcern;% {7 S5 c9 }, o; y0 n7 K% ~7 n8 U
  6. use MongoDB\Driver\Query;
    ! o* F. L4 N5 a" D, P, i
  7. use MongoDB\Driver\Command;
    1 W0 Y5 z5 I/ y. P

  8. ' i0 u6 u: \) ?2 X2 y& ^' A
  9. class MongoDb {9 W9 `( u- [7 O5 p

  10. 7 B# z; z2 d; u, Z8 I6 I# G2 R
  11.     protected $mongodb;& W: D5 t2 p$ G9 W; z! g6 A
  12.     protected $database;
    6 D: r5 c8 l- e
  13.     protected $collection;- A+ i* D# i7 K$ }% }4 v% @
  14.     protected $bulk;( V7 X5 r: N* C5 x# Q2 x6 e
  15.     protected $writeConcern;
    ( w" p/ W( i4 O: p7 V' o# `
  16.     protected $defaultConfig( O  x3 m- f5 z* u; Q
  17.         = [
    5 r& }3 X( j1 Y% A
  18.             'hostname' => 'localhost',6 w9 [2 r* `  _+ f
  19.             'port' => '27017',5 Z5 V7 A1 }9 `) {2 b2 X% e& ^, g
  20.             'username' => '',! W. c/ {% ?. u- A  R8 o  C
  21.             'password' => '',, }% k$ X& x% T: v$ Y9 a5 l3 ^
  22.             'database' => 'test'
    0 L) ]+ p1 d! e
  23.         ];6 U" i, y$ _9 q- i$ F+ G7 A! g

  24. 5 X! V( [  J7 U: C0 w  Q- f7 R
  25.     public function __construct($config) {
    0 [/ ]! A5 u& [
  26.         $config = array_merge($this->defaultConfig, $config);$ B. ?$ c- @& B; P6 G% h/ }
  27.         $mongoServer = "mongodb://";
    - @- X3 k, r$ I, Z- A% {  I, {
  28.         if ($config['username']) {
    ; Q' E0 s% W1 l
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    3 D8 T" v2 i1 M+ s6 q* s5 g
  30.         }
    + ]7 Q4 _) l8 ?; m' O4 t5 L
  31.         $mongoServer .= $config['hostname'];2 K4 [0 Z3 y0 ^3 c" w' ~/ K
  32.         if ($config['port']) {
    & M4 T. Y* A. [6 F- Y9 J7 [
  33.             $mongoServer .= ':' . $config['port'];
    ; X0 G4 x2 T& e* v- q4 S
  34.         }2 X1 b% W1 _$ Y9 `+ V% w
  35.         $mongoServer .= '/' . $config['database'];
    5 S7 L/ ?, h7 q# Y2 S* B( {
  36. / p/ Q; G3 o+ ?1 b# y
  37.         $this->mongodb = new Manager($mongoServer);- I3 l# g- S8 g
  38.         $this->database = $config['database'];
    % v, P6 c$ ?; s: J" X1 D
  39.         $this->collection = $config['collection'];
    1 e& W7 m& E5 s: y, @+ w# F
  40.         $this->bulk = new BulkWrite();
    ' ^' l) N! q% l4 Y0 E0 \, R$ R1 @
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
    ) I  @4 @% l  A; l* V
  42.     }
    + W" R% _8 S% a9 y

  43. ) K0 Y  d( |, Y6 k, G  F( L
  44.     public function query($where = [], $option = []) {
    1 t- ?( z4 M8 I; p' Q
  45.         $query = new Query($where, $option);
      f3 U+ {$ k: F+ z6 T
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    - d* p' w4 E& B" l0 L. u

  47. * I$ X8 U  \. A
  48.         return json_encode($result);
    5 n0 ^: J/ O+ d8 O7 O' E/ F! T6 T
  49.     }/ R+ M. y& y1 B/ o( X8 U, z, z
  50. % G$ M2 q2 {0 O( x: H9 s
  51.     public function count($where = []) {1 n: l! H: ]3 b$ B! D& \) g' V3 t
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);- x2 @# ?$ E& U0 [- f! W
  53.         $result = $this->mongodb->executeCommand($this->database, $command);$ l! o# f- m; `9 R4 l( p
  54.         $res = $result->toArray();
    ' `% s4 l* @: V
  55.         $count = 0;
      z/ C/ Q" y/ E" k: F/ ^* e
  56.         if ($res) {8 q3 ?' d* W+ b2 y, z( L6 u! M
  57.             $count = $res[0]->n;
    ' A9 T) h9 p+ S; N, n. \
  58.         }( l: T7 f; a/ J+ j4 }3 Z4 Y* L
  59. 1 o7 Q. [$ u# C) O! w
  60.         return $count;3 L; H5 m: X6 @1 q2 C* {
  61.     }
    + P* P: a" b) T& W# i

  62. ' s6 w/ l0 i1 b& x
  63.     public function update($where = [], $update = [], $upsert = false) {) y( M- K. O1 Y. |
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);4 i8 ^6 U  E2 p1 G" l$ ?
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    . K1 y7 q/ B% q
  66. . m) g4 G% _; o8 ?# S
  67.         return $result->getModifiedCount();
    , c7 y$ d0 F4 V( o
  68.     }/ G6 c# [- T1 P/ N2 u

  69. 3 _5 }6 q  \4 C" c4 @" w( c- H
  70.     public function insert($data = []) {
    ) D1 m8 X7 n; T( z( f9 S
  71.         $this->bulk->insert($data);
    + Y$ T0 W& A) B; j/ L
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    : P' V/ h+ |) j+ m! `  h/ Y5 n! T0 V
  73. % f  r- r, k4 B- J
  74.         return $result->getInsertedCount();2 J6 s2 p+ e1 N* u; K
  75.     }
    3 j7 D9 A# \+ u5 W
  76. 7 [+ o6 i8 I* Z/ D0 u$ O  X
  77.     public function delete($where = [], $limit = 1) {+ {# t4 m/ ^7 Q" _- `6 p
  78.         $this->bulk->delete($where, ['limit' => $limit]);8 W# _! C7 e! X$ S  r$ v
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);# z; [5 {: K* \2 g) ]  |/ q
  80. ) E( _4 v3 M4 o2 m# L
  81.         return $result->getDeletedCount();7 |5 b1 t1 Y( R: v* z+ V6 O
  82.     }
    4 l* \. i$ G! v5 j7 d
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  •   x9 p7 m% J8 I* q6 S$ i
  1. new MongoClient();
复制代码

  • . O6 A' R- F0 l( R/ j. `
  1. new MongoDB\Client();
复制代码
2.新增
  •   `, X! s( p- q+ B1 J0 P6 x& G2 E
  1. $collention->insert($array, $options);
复制代码
  • 1 l' u$ B, o- c
  1. $resultOne = $collention->insertOne($array, $options);//单
    + W, U- F. f# S% p- P0 Q# t* K
  2. $lastId = $resultOne->getInsertedId();
    2 C- V# Z+ U, X' ]2 c( T  h
  3. $resultMany = $collention->insertMany($array, $options);//多
    3 }2 M1 Q4 v, C- Q5 O( k# w. X
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • 7 `) s/ W6 M9 J. U* g
  1. $collention->update($condition, [/ F, R, N. `: g9 ^
  2.     '$set' => $values
    # ]- I: q; O$ [3 U. s
  3. ,[
    $ E" t; J" h3 A' _% P
  4.     'multiple' => true//多条,单条false
    ! r: z& H% Z' P; s1 ~
  5. ]);
复制代码

  • / i8 ^% ?1 V. i: T" M3 n7 T5 Q
  1. $collection->updateOne(
    - [" ]' r6 h$ u& a
  2.     ['state' => 'ny'],
    2 P5 X( ?9 [+ i4 C$ q
  3.     ['$set' => ['country' => 'us']]
    4 g6 C  x* o2 O' K  C
  4. );
    4 q: n, ^8 `4 m2 X
  5. $updateResult = $collection->updateMany(
    * n9 r' I+ `/ m2 Q
  6.     ['state' => 'ny'],0 c) P  Z! \* b$ i# `& T
  7.     ['$set' => ['country' => 'us']]
    $ i6 L; H% D5 T* G! Y! c1 z9 _
  8. );
    % c* {2 C' d/ x
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • 6 Q+ k8 i, S& K3 q
  1. $cursor = $collection->find($condition, [$ b% @, M/ U9 k% |
  2.     'name' => true//指定字段
    ( }  Q& t  r, N5 g  a: p9 j
  3. ]);
    ' c2 R# a* {: T; Y# _
  4. $cursor->skip(5);
    : l: F+ A+ K# l( L+ K
  5. $cursor->limit(5);
    & g3 M. s! }/ N2 x' |  E- j$ j
  6. $cursor->sort([
    7 M$ E/ |0 t: v  _/ K" N
  7.     'time' => -1
    + }3 h" F8 [  X5 j: S
  8. ]);
复制代码

  • ( z9 U5 e7 u) w) ?8 X# ^7 Y
  1. $cursor = $collection->find($condition, [" [# r4 V2 q2 ^' t; x" j" Y
  2.     'skip' => 5,
    6 q0 T" b- x8 n! B* H
  3.     'limit' => 5,
    5 h  L7 u7 y. X
  4.     'sort' => [
    ' j; O$ @5 x' Q9 {0 V6 B+ z
  5.         'time' => -1
    ) M2 T$ G; ~  V2 l" s* }
  6.     ],//排序
    ; y0 s) J& ^9 S7 E
  7.     'projection' => [
    . u6 p( E- e. O7 q& U
  8.         'name' => 1//指定字段
    , J& _/ H+ |- O3 g$ [2 T
  9.     ]
    $ z, t+ M; C# r* L7 r
  10. ]);
复制代码
5.删除

  • - m  L# _% D$ D' E' L$ ]
  1. $collention->remove($condition, [
    ; S( a- L4 ^1 p
  2.     'justOne' => false//删单条; J; z+ c) D3 O2 m
  3. ]);5 j9 v' o3 S& z
  4. $collention->remove([]);//删所有
复制代码
  •   |- B& M( F: C
  1. $result = $collention->deleteOne($condition, $options);# @* P! @) I$ P/ N
  2. $collention->deleteMany($condition, $options);! Z# V. {: P5 b0 k) z* j

  3. 7 u' z' q$ \4 ^- d
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([( _, l: x5 G+ X4 }
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键: v; S" ]: }6 G
  3. ], [
    : s. u) h5 r" {( x
  4.     '$inc' => ['id' => 1]//自增: {( C( y! n$ e1 a
  5. ], [% u9 _5 P. C2 k$ _% R* l
  6.     '_id' => 0  i: K  S& `& a2 V
  7. ], [
    % V3 g! d3 e# T, O- C
  8.     'new' => 1//返回修改后的结果,默认是修改前的$ `' A, ~4 T) j
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([& t# o- m; ^3 [' R/ _- d6 U5 G
  2.     '_id' => $tableName
      Y0 \, n4 t5 ^: a& f3 o
  3. ], [
    - o1 N: e, y4 B4 N: I3 `; H: @2 e* ^
  4.     '$inc' => ['id' => 1]
    7 b  ~2 W9 H+ \2 p/ V7 L( V7 L
  5. ], [
    - [- Q$ g3 {5 Y8 p+ ]6 @1 Y9 c
  6.     'projection' => ['id' => 1]," f: f- H5 I  @4 m& D
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    $ L+ \2 J9 ~/ G: J7 Y" P" s, w7 X; f
  8. ]);
复制代码
! A7 C: Z2 {0 L4 a2 F  g
( u8 @+ M. ^6 M9 J; P
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 12:44 , Processed in 0.060468 second(s), 19 queries .

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