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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11546|回复: 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  z4 I) J: y7 T2 }2 D$ H

  2. 9 J: M" e# C/ f3 ?( q+ Z
  3. use MongoDB\Driver\Manager;
    $ U* q# e2 C/ Y; n. V5 C
  4. use MongoDB\Driver\BulkWrite;
    : N0 v% @( `+ x5 _& W8 e1 d/ W$ L" X$ Y
  5. use MongoDB\Driver\WriteConcern;9 `8 n8 Y& x% s- \
  6. use MongoDB\Driver\Query;
    , t0 N1 f' @) D. O( b9 u
  7. use MongoDB\Driver\Command;
    2 I( w, r  x' e8 F% E" n2 g

  8. ) p( g5 l; v+ D+ l! `
  9. class MongoDb {' i; ^5 \+ @' C& {: a& |

  10. / w" Y" _$ D$ N; h
  11.     protected $mongodb;
    ) ^% Z8 ^5 p4 `! X) x
  12.     protected $database;2 Z7 V; w. W. ?$ A, \1 J
  13.     protected $collection;$ m: z7 F9 J; [# P& x* `0 B1 A( J
  14.     protected $bulk;1 h( F& ]3 P0 G. Z; j$ i
  15.     protected $writeConcern;3 G6 W' T3 }# }3 n5 f+ o( u; x1 g
  16.     protected $defaultConfig
    " L- H8 }, D0 o/ M
  17.         = [1 ]( k% v! g# t# q! E
  18.             'hostname' => 'localhost',
    3 O0 n& M3 g: z3 l3 _/ n
  19.             'port' => '27017',
    4 b; D" r/ T: k6 \0 r! X& u
  20.             'username' => '',2 c6 J- `( y, f" Q! o# s
  21.             'password' => '',
    7 P& ]" |+ S6 \6 w4 |" ]8 S: j3 `; P
  22.             'database' => 'test'7 D0 Z- Z& q. R
  23.         ];6 ?: h# E1 L- w- A# `. |
  24. ( ?! P% b& B) b4 g9 @1 t7 m
  25.     public function __construct($config) {3 h3 Q2 H* u: [* K; x7 C1 |, M
  26.         $config = array_merge($this->defaultConfig, $config);# S, I) f4 T  n9 S3 C# S4 G
  27.         $mongoServer = "mongodb://";9 ]- t- J: ^" I  A8 d
  28.         if ($config['username']) {6 c% c) ?  B4 H# t) R* i
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
      q! }. m/ J# ]& C# K1 X: h
  30.         }
    0 U( H1 O8 W  I$ f) M- Y
  31.         $mongoServer .= $config['hostname'];! h* o3 Y0 h4 I
  32.         if ($config['port']) {* v  W; O9 ^7 t
  33.             $mongoServer .= ':' . $config['port'];
    " s( p2 }, W: I8 u& r/ k0 e
  34.         }) E; j) x& q1 _+ m5 x! P$ g& s! C
  35.         $mongoServer .= '/' . $config['database'];9 ?& x5 l: m3 @5 Q  C8 Q/ u9 A

  36. + S/ S# c  |3 G! I( [: U! Q/ k
  37.         $this->mongodb = new Manager($mongoServer);
    : e, @7 P  Q4 q; p
  38.         $this->database = $config['database'];, c+ z* o3 F9 T# C' K& [
  39.         $this->collection = $config['collection'];
    * Z: w" L, }3 H& \& n4 T" T
  40.         $this->bulk = new BulkWrite();% b& c# o/ j  N8 c- A9 G
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);: o3 P8 K! Q) S% K, {% K% P6 a
  42.     }1 Z- ]: M7 H/ U( V/ G* k1 u1 \9 _

  43. 6 K2 [# T0 L9 u+ N
  44.     public function query($where = [], $option = []) {
    $ p6 d9 N1 x* A5 n" k2 w4 D! ~; R
  45.         $query = new Query($where, $option);1 X* v- E5 Q, C, I
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    & N% e1 b1 ]; C# G- d5 v# R
  47. 6 h4 ]2 Z$ e5 Q/ V+ C7 [
  48.         return json_encode($result);
      P3 x8 k" z/ u9 M6 P$ M
  49.     }% e* p8 y2 n  O! @2 V+ Z

  50. 8 q' f8 H3 N* q# {" V
  51.     public function count($where = []) {6 h4 \1 M; B) Z' b3 q  C4 R
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);( _+ P8 F8 h8 H8 i
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    $ \9 [* \& [/ n; {# j
  54.         $res = $result->toArray();7 D& P0 Y1 w+ ^6 N& r7 T! K
  55.         $count = 0;
    4 i  M" b! U# t7 w+ @* C( _
  56.         if ($res) {  a! i- i( f% G, ?* c2 q  }: e. l! ^
  57.             $count = $res[0]->n;0 `( I, t6 h1 s8 o* s+ i; b
  58.         }9 e4 D! E5 p9 s6 ^' {
  59. 7 a/ R3 f' \1 ?6 D
  60.         return $count;4 l4 y* r3 W* b
  61.     }
    4 V  N0 \' f) ]  y4 `

  62. 1 @. g, V7 L0 \: d" o! x
  63.     public function update($where = [], $update = [], $upsert = false) {" G8 D8 S8 o2 F! N) b% v
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    4 H, w# E- u+ i' n4 C4 X( f# X  S
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);) `/ I/ M! }, s* K4 p; f
  66. % I# O7 v" R/ q# i" \4 I; j
  67.         return $result->getModifiedCount();
    # n% j, M- J: v( v* A& z
  68.     }
    * z" r6 S: i; _- t- T

  69. 5 N2 V7 P) t9 L: q8 q
  70.     public function insert($data = []) {- n5 O' O. K5 f! e; H
  71.         $this->bulk->insert($data);
    9 L( @3 P! h2 j6 l3 R
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ; {) V( l* @' V# O; P
  73. 8 m, W* f; p2 J( P
  74.         return $result->getInsertedCount();
    $ i, G- Q5 o" C; B
  75.     }
      }+ D+ @6 t) h3 O* c+ F4 e( l- t, _
  76. ' D6 V% h, A1 \
  77.     public function delete($where = [], $limit = 1) {2 X$ J) K1 p+ X( I9 Y
  78.         $this->bulk->delete($where, ['limit' => $limit]);3 q9 N5 N( p4 T
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    * c( N8 C1 D6 U

  80. / W5 K- o- c( F& P
  81.         return $result->getDeletedCount();8 \5 q/ |3 b( E/ p$ ~8 N* F/ p
  82.     }$ ~' {& L. F3 w; a) P5 }
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接

  • / Y; W3 B0 i! e7 `
  1. new MongoClient();
复制代码
  • 3 s  z5 _4 T* _* m8 Z
  1. new MongoDB\Client();
复制代码
2.新增
  • ) R" ?, `, N0 V( k( Z, f, m
  1. $collention->insert($array, $options);
复制代码

  • 1 n- v8 P& w/ B. U/ y6 Q
  1. $resultOne = $collention->insertOne($array, $options);//单
    " y3 L# n1 o" O! A9 [# r6 _& v
  2. $lastId = $resultOne->getInsertedId();
    + c0 [0 q9 X/ M8 P  A$ L
  3. $resultMany = $collention->insertMany($array, $options);//多
    4 S/ x+ ?2 W; ~: z: d7 `4 i
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • - `) O0 n/ ^; H6 S
  1. $collention->update($condition, [
    . Z1 m, i0 q" L1 T; }+ t
  2.     '$set' => $values9 n( B9 r, ~4 Z7 r
  3. ,[
    7 Q5 q$ n7 }+ l. T; ~
  4.     'multiple' => true//多条,单条false* w1 i/ N* u; ~( b1 S7 B
  5. ]);
复制代码
  •   {8 i& }+ Y: N+ H
  1. $collection->updateOne(6 ~! W2 o# T5 f1 d
  2.     ['state' => 'ny'],
    5 Y. J* g' C" K' V- [
  3.     ['$set' => ['country' => 'us']]% J  D6 L/ Z6 ?, j$ R7 ^
  4. );) i) T- G0 l: o9 p
  5. $updateResult = $collection->updateMany(/ B" f8 b- A" h$ u: d& B$ ?: ~
  6.     ['state' => 'ny'],
    , C* I3 u/ X8 i8 o' _7 s" y
  7.     ['$set' => ['country' => 'us']]
    , h1 o5 x0 @. u
  8. );
    . Q! q3 z" D, m$ Z1 Z4 V
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • - ]( E- o) L8 |- D& n3 M& _1 q
  1. $cursor = $collection->find($condition, [
    % D$ A1 R$ D9 k0 Q  j
  2.     'name' => true//指定字段5 z7 n5 g" N6 z5 D
  3. ]);
    , g. S+ g+ O1 X3 S+ E
  4. $cursor->skip(5);
    & X- B+ m' D! |* m, f
  5. $cursor->limit(5);
    9 l$ f6 p) M, T
  6. $cursor->sort([
    ! d; w  ^  |5 k5 q7 ?
  7.     'time' => -1! A6 K4 J3 M$ }0 ~
  8. ]);
复制代码
  • , y1 w5 R4 E( |5 |+ \
  1. $cursor = $collection->find($condition, [+ k/ a! k7 N* b7 V: H) G4 l8 V1 _* U0 y- w
  2.     'skip' => 5,1 Z; @% V* |* d5 I7 E* `6 k; Z; U
  3.     'limit' => 5,
    + b- Z: A8 }+ C9 @0 `5 S- ~( g
  4.     'sort' => [
    : w* e' W, @& Y1 J
  5.         'time' => -1" m* V: ~! w! s$ i
  6.     ],//排序7 X  Z0 [/ y: c. q8 d! t6 {
  7.     'projection' => [
    5 Y" M3 q, G- a2 ~5 w
  8.         'name' => 1//指定字段
    & y% n; R- C5 s1 l( L6 c) U
  9.     ]
    1 Y2 U. M4 q$ p4 E
  10. ]);
复制代码
5.删除
  • , R( k8 r+ `4 }
  1. $collention->remove($condition, [
    , A& n: a. W0 l
  2.     'justOne' => false//删单条3 Q1 G- l% i, f* C9 v$ x
  3. ]);0 G% f. V; S0 e; f
  4. $collention->remove([]);//删所有
复制代码

  • $ I3 J/ n2 W' d$ k0 h1 z# ]$ F5 O- d
  1. $result = $collention->deleteOne($condition, $options);
    6 P4 e9 r- I/ R! J$ a& c
  2. $collention->deleteMany($condition, $options);
    + p* \( t6 S- _
  3. 2 v4 _, u/ o1 P: ^
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    6 K, B$ }4 v& V, N: Q! B! |& k& G
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键' O7 ]5 \2 a4 G4 \
  3. ], [7 U3 }' R- s! R7 o$ V' X* i
  4.     '$inc' => ['id' => 1]//自增4 @6 N& B+ o- V; |: j
  5. ], [
    % n, p( V) `; `+ B0 F
  6.     '_id' => 0
    : R0 I& p4 `0 ]2 _, I
  7. ], [
    & J6 W+ {# m( o" d
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    ! v* }) w- k$ o0 Y, ~; V7 _( h, ]
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    ! g! X! }. h7 \7 Z) b) t
  2.     '_id' => $tableName
    ! y) B: W8 R& ^8 m2 [/ j+ B
  3. ], [6 f2 |, ?' O* x. ]9 q: M
  4.     '$inc' => ['id' => 1]
    7 i" h; B: k9 K) L, h
  5. ], [6 ~8 C/ }2 E* R8 M8 D  @: U
  6.     'projection' => ['id' => 1],; r) B& k+ Y% a4 I& A! x
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    ) n$ |/ @1 \! @& A
  8. ]);
复制代码
1 e1 w! k7 e6 a1 _, e! a

( S, M3 X9 n& C6 ]$ V
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-20 05:39 , Processed in 0.144405 second(s), 20 queries .

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