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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16550|回复: 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
    ! Y" ]  ]& O% |1 ?7 m
  2. ! |2 J' ^. ]/ ~: }
  3. use MongoDB\Driver\Manager;: m" c7 O! p7 ?
  4. use MongoDB\Driver\BulkWrite;
    6 B" y4 P% B+ a! g3 m7 A9 H
  5. use MongoDB\Driver\WriteConcern;* {: T( O/ L4 C3 d, G( x
  6. use MongoDB\Driver\Query;
    7 f) f3 l+ H5 V5 A. U% T: f2 D
  7. use MongoDB\Driver\Command;
    2 S5 ^) q; A- `

  8. 7 ?) k9 b2 [* J: E: C& C
  9. class MongoDb {! J) t/ |* m, m" w( z! ~7 y) L" `( J

  10. ; M% B; w% R; C' I& {, j- G8 p, o4 J
  11.     protected $mongodb;0 b" E. T9 {$ P: i  P+ Z( N* z- t& \0 {
  12.     protected $database;! v8 ~6 X' {6 N1 Z1 R9 m
  13.     protected $collection;# [) K4 x. g  R0 z- j, r
  14.     protected $bulk;6 G- `: i$ r0 E' I1 ~8 d
  15.     protected $writeConcern;
    9 R+ j7 X. I- e+ T# E0 h% T; e  ?
  16.     protected $defaultConfig
    9 u; L" F3 p* @  ~: V5 f! x) F1 w
  17.         = [- Z3 T7 M+ u2 k. |
  18.             'hostname' => 'localhost',
    3 \! g+ z5 z0 _6 a6 H
  19.             'port' => '27017',- w. ?9 l, u$ H. @
  20.             'username' => '',
    / b1 M  E. i' G# w/ v# }  p
  21.             'password' => '',
    4 b% Y/ s! k8 L, l4 M; t
  22.             'database' => 'test'
    4 i1 F1 r" G' K# R8 g0 y3 l& ]
  23.         ];3 J/ ]8 F, F$ K2 i( u
  24. % S  O2 L3 S# w/ X3 X9 M
  25.     public function __construct($config) {. `) y6 w) t! y# E' T; J7 t
  26.         $config = array_merge($this->defaultConfig, $config);3 `9 h/ ^9 ]( ?1 w
  27.         $mongoServer = "mongodb://";1 h' ]6 F8 |3 |2 y! [: j* U
  28.         if ($config['username']) {
    2 d! |3 k- ~5 \3 e5 u2 f
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';, p0 x5 _1 x# b2 a# v
  30.         }3 s* x+ Z( W& U0 ]; l8 Q: g
  31.         $mongoServer .= $config['hostname'];
    ) N  @. \( C8 ]0 }4 l9 D
  32.         if ($config['port']) {5 t; {/ I% A2 D/ H. G- W
  33.             $mongoServer .= ':' . $config['port'];
    0 v+ r, p6 d+ z" n  [$ x% A, u# g
  34.         }
    . d8 t- O, |" @! k$ S( H8 i' {2 Q
  35.         $mongoServer .= '/' . $config['database'];
    + `+ p- v* s  X; S9 U

  36. . L9 W, h+ m  z+ t
  37.         $this->mongodb = new Manager($mongoServer);
      a: E1 N" N  z) a1 h
  38.         $this->database = $config['database'];
    5 d. }; x$ x# M/ `3 b" {8 x
  39.         $this->collection = $config['collection'];
    1 _6 T/ U' `0 ?+ D7 _- X
  40.         $this->bulk = new BulkWrite();; K) n/ t9 ]8 i" S
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);& h, F, f: V5 @* R2 U6 c) v& q
  42.     }
    , L) @" T. q: f6 d5 Q8 j
  43. ' c+ _: M) R9 r% i- H, G
  44.     public function query($where = [], $option = []) {
    * M- S& o# `4 {0 v0 }+ O6 f
  45.         $query = new Query($where, $option);  K! E' `' N/ A8 X
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);- ^- j$ c, q5 x4 t/ Y# o. u4 T; Q
  47. 8 \# K9 y, k# |* v4 i
  48.         return json_encode($result);
    # {0 k/ f. R7 i5 C0 ~
  49.     }  k: _* F6 l6 T! n- z
  50. 0 d* G5 A; \  ]
  51.     public function count($where = []) {4 }; f6 d/ y" o/ q( i7 j
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);# V5 @; x$ T8 k0 l
  53.         $result = $this->mongodb->executeCommand($this->database, $command);
    4 R  ~) L5 H# ^! a% S
  54.         $res = $result->toArray();4 h0 g% V  h" m0 @* U2 y
  55.         $count = 0;: f1 p% J# z( ]
  56.         if ($res) {
    8 Y7 r% J" q) j+ `! y
  57.             $count = $res[0]->n;, {$ x% O4 [' X. i6 O6 n: C; e
  58.         }
    " f' a( b# n; l: `3 @0 I, V

  59. 8 J! F% w0 W" a/ _$ Z  }
  60.         return $count;
    ( H: D$ R& P8 t0 @6 t/ W9 M1 o
  61.     }
    9 p1 N% l9 ^( r+ r
  62. ' |  Z* d" |: G2 D/ ?5 }
  63.     public function update($where = [], $update = [], $upsert = false) {
    % k) V/ ^" ^. o3 b* ?7 w( V% T* |" K
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);+ S' N3 `4 m. p5 @
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);4 J6 a. Z0 c9 b

  66. 4 q$ ?- l# p- G. d. u+ C
  67.         return $result->getModifiedCount();: G3 G5 ]9 P: X. C# }$ G
  68.     }, |6 d. f! e6 {& M
  69. 5 k1 D5 _6 e9 y
  70.     public function insert($data = []) {; z! J( B/ Z! g
  71.         $this->bulk->insert($data);" }( r/ y$ _' h4 D  Y8 Y: ~
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);. J5 K8 h, [9 O/ R5 s1 K

  73. & R0 R; }; I# t4 h
  74.         return $result->getInsertedCount();
    / G0 n* C" J+ X" o9 W9 o# j
  75.     }( B; J4 _1 c' o2 G: d- k8 Q
  76. . V; V* F; x( N* l" P
  77.     public function delete($where = [], $limit = 1) {: B) h% z6 p$ G4 r8 z+ x+ u" C
  78.         $this->bulk->delete($where, ['limit' => $limit]);
    9 M% V) F4 s2 ?; G) q
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    0 T5 C- Z* ]) U, V9 J
  80. % x" @3 i+ \# m; v- P
  81.         return $result->getDeletedCount();
    3 l% w. A2 z7 T2 M: e% y- P
  82.     }
    0 g4 P6 e% o4 T/ T* a- m# Q6 r
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • * U9 z) u1 s4 Z/ @+ |
  1. new MongoClient();
复制代码
  • 8 \: I4 j; {) L! Z* z
  1. new MongoDB\Client();
复制代码
2.新增

  • % F' ~9 C) y! v6 R& v5 T
  1. $collention->insert($array, $options);
复制代码
  •   r5 \8 ]) X& `/ i5 j& H2 H
  1. $resultOne = $collention->insertOne($array, $options);//单
    ; C" ]: M6 B8 B( F& V: x! b, p- C
  2. $lastId = $resultOne->getInsertedId();
    9 u1 i$ R' u( i4 Y
  3. $resultMany = $collention->insertMany($array, $options);//多
    ) T0 H# c) |2 u
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • ' X0 B/ J2 e) `. i& M" d
  1. $collention->update($condition, [
    # _; i) N' [0 A) [( s1 F$ b
  2.     '$set' => $values/ H! c. z- M; N# l% Y& j
  3. ,[. [  r* I6 _7 g4 c2 l* _* W
  4.     'multiple' => true//多条,单条false$ g: j) |" [; P
  5. ]);
复制代码

  • 8 C9 o( L+ m' }  b2 |
  1. $collection->updateOne(& s2 K; b' }: g2 {9 d& X. Y6 t
  2.     ['state' => 'ny'],! R8 L, O) ]! o
  3.     ['$set' => ['country' => 'us']]2 ]' M& ]2 q. `
  4. );1 X) Q; ~1 Q2 {, t5 J$ X- w
  5. $updateResult = $collection->updateMany(
      U6 @6 l$ F" W: C- Y  ?, b
  6.     ['state' => 'ny'],. m  E; W* A0 u, X# I  N
  7.     ['$set' => ['country' => 'us']]* |; D1 e* `) G0 M' z' c2 |2 ]
  8. );) d1 r- n% S/ U( l1 y; i
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询
  • % d% d2 N! O9 s* N. |9 {
  1. $cursor = $collection->find($condition, [! z1 N) G! A* w7 c% L% F; R
  2.     'name' => true//指定字段: b% y$ O- i2 `/ ^: `, \9 Z
  3. ]);
    / ~/ N. B% U3 F4 G$ B
  4. $cursor->skip(5);
    3 ~4 e3 |! N$ H5 Q" C
  5. $cursor->limit(5);
    , F4 K3 {8 K' |' p; I* G
  6. $cursor->sort([+ n0 v0 C. D! T! V7 c
  7.     'time' => -14 K6 T6 U) B# |9 Z1 b2 z
  8. ]);
复制代码

  • , b- o4 T. C7 w+ ^
  1. $cursor = $collection->find($condition, [" F$ h4 Y* a! ^% K8 u; G0 w
  2.     'skip' => 5,
    / {# i, U* S+ g1 W& N
  3.     'limit' => 5,
    , q0 C; s# F* O( b0 j9 l
  4.     'sort' => [% f) s& Y% e. i. k' C* ]
  5.         'time' => -1- a1 N4 j# i$ g
  6.     ],//排序
    2 L/ N9 o3 B# {3 p  F" z' j
  7.     'projection' => [' I+ J/ x7 N: }  |
  8.         'name' => 1//指定字段. H; j; f) g7 u+ c1 D; }  p+ T
  9.     ]: i7 m- {" @  s
  10. ]);
复制代码
5.删除
  • ) @- s2 z' U  F. g& A
  1. $collention->remove($condition, [5 k1 J( U& j# |  Y  y$ `8 H2 I- F
  2.     'justOne' => false//删单条( B0 z4 o1 l; F7 K( K6 |
  3. ]);% @; \" }3 Q5 w; h7 b8 {+ y
  4. $collention->remove([]);//删所有
复制代码
  • 9 R* [" t# U2 R7 a% |" R% q+ b
  1. $result = $collention->deleteOne($condition, $options);( q+ Z9 w0 O- c' {4 c* |3 u; h7 s
  2. $collention->deleteMany($condition, $options);. [' ?6 S0 d" i' `( g3 v6 m

  3. 3 P, c& \& c; }. U) T7 k
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([0 W8 Y+ D  J" n7 R9 k" n# e
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键
    1 e2 u/ E  d: a9 q7 A! P+ L' C
  3. ], [" s! q2 O/ V, V8 d3 U0 u
  4.     '$inc' => ['id' => 1]//自增
    8 z# p) a9 p; L* O
  5. ], [
    1 s. X" l; w3 }+ z* L6 a5 X# L
  6.     '_id' => 0
    1 m( T* l+ N# j% r7 S4 F9 |1 O
  7. ], [
    * ]1 o' c" u. E7 Q  S# B' V& [  V
  8.     'new' => 1//返回修改后的结果,默认是修改前的
    . q" W) x! J" G0 w
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    6 X, O1 D- L) q4 r
  2.     '_id' => $tableName* ?' `* n) C; `: T( ~, w
  3. ], [
    * N1 K3 D7 F1 o: I. X* L
  4.     '$inc' => ['id' => 1]6 O4 \$ ^; h2 E2 c) L+ K1 p
  5. ], [  x+ @5 y( x& @' L
  6.     'projection' => ['id' => 1],9 ]7 \2 i7 T4 X* i; j5 U' T
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER/ G% w8 s0 I- q9 B
  8. ]);
复制代码

0 l6 B% {& A) s, I
/ S1 i: Q# o' v- h
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 03:25 , Processed in 0.057511 second(s), 19 queries .

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