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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16357|回复: 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
    4 r  W/ U  x- d

  2. 9 M, W% }. ]" q
  3. use MongoDB\Driver\Manager;
    & G7 D3 C% |# h, V( X/ ^- {
  4. use MongoDB\Driver\BulkWrite;+ t+ n: Q: c) u8 H
  5. use MongoDB\Driver\WriteConcern;6 M1 H5 H6 O4 [9 O7 I
  6. use MongoDB\Driver\Query;: x3 |7 ]( `( r
  7. use MongoDB\Driver\Command;2 o8 b( _9 V! u6 Y6 r

  8. , h5 L4 x* q! b  I$ m2 A# O
  9. class MongoDb {
    1 D$ B, J5 `  ~/ O
  10. 7 b0 \! M  j# u3 J5 m2 o. ?( ^
  11.     protected $mongodb;
      X/ q) [; E  C
  12.     protected $database;8 _) P0 M5 q. S4 ^' E8 y
  13.     protected $collection;
    1 j2 f$ _9 x* ]3 n
  14.     protected $bulk;
    ) |1 \% j- L) d% O+ E: J9 w) K# T
  15.     protected $writeConcern;/ l) ?3 M# ]) c1 c- g
  16.     protected $defaultConfig
    ; X, \/ i, _+ c# ]% [
  17.         = [
    4 ~; t6 n# A8 {4 e0 u
  18.             'hostname' => 'localhost',; W4 w# @9 l+ L1 G: Z$ D
  19.             'port' => '27017',
    " ?: O: B+ p2 b1 K7 [4 p
  20.             'username' => '',
    * g3 ]0 S5 `  n2 e1 U8 M
  21.             'password' => '',
    ! q- p  E  {7 ]8 C: B9 k: S
  22.             'database' => 'test'+ G/ Z# R2 z+ q2 W; M
  23.         ];" ~$ I5 @8 J. u5 n+ b
  24. ' G0 d6 x* V/ Z2 @0 W
  25.     public function __construct($config) {
    ! X) ?: q" @' `8 n2 H' J
  26.         $config = array_merge($this->defaultConfig, $config);
      y# t& u7 b% O9 a& x
  27.         $mongoServer = "mongodb://";/ D2 y2 g2 B- C( B/ J+ M, k; |# c4 x
  28.         if ($config['username']) {: Z9 N9 \& a, W8 P
  29.             $mongoServer .= $config['username'] . ':' . $config['password'] . '@';
    0 b4 C) N$ w: l3 g7 B* {
  30.         }/ Y. K- J! ]; U, |  Y7 {
  31.         $mongoServer .= $config['hostname'];+ O- b' u3 f. D0 K
  32.         if ($config['port']) {
    # w7 x' L  V0 d7 ~6 D$ l
  33.             $mongoServer .= ':' . $config['port'];+ V& V, [4 }$ s7 g- R
  34.         }
    . l4 Q! y! t( `0 |- P  O
  35.         $mongoServer .= '/' . $config['database'];
    + h" S7 e" S& k  m' Y" a4 T- \- j

  36. * e7 i, ^6 k1 T
  37.         $this->mongodb = new Manager($mongoServer);& L/ b2 K  m0 [$ \. \4 y  e
  38.         $this->database = $config['database'];& {; |3 s& A% o/ H" i& p- x1 R: R: {
  39.         $this->collection = $config['collection'];, i, C9 a4 r$ ~5 E6 h$ R
  40.         $this->bulk = new BulkWrite();
    : X6 ]. _+ ?' @- y$ _& |
  41.         $this->writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);( l  `: P1 I1 {2 x: N0 b
  42.     }0 D! V) u% l- x/ U: p
  43. , e& |* [# f! e: x) ^3 e  y
  44.     public function query($where = [], $option = []) {
    # y2 x1 K. B# W2 ?2 p5 f; t
  45.         $query = new Query($where, $option);
    ! P  T. r) p$ O$ l! [6 n9 R" d
  46.         $result = $this->mongodb->executeQuery("$this->database.$this->collection", $query);
    + t) {2 [" P9 L% A% f+ @' W- O8 x

  47. 8 I0 U# t% N4 m( b
  48.         return json_encode($result);
    ! `2 k- M" \! v9 t' Y: L; c' ~
  49.     }
    , l$ M5 X0 B. W3 I( W& N

  50. ! H* ^* v' W; `4 l
  51.     public function count($where = []) {
    1 N  [! O) h! Y0 ]
  52.         $command = new Command(['count' => $this->collection, 'query' => $where]);* ^0 g& F' D+ ~6 d# ^9 i" Z3 I
  53.         $result = $this->mongodb->executeCommand($this->database, $command);# `; p- T) ?% _  d
  54.         $res = $result->toArray();2 ]4 P, q* g% n* J6 E$ ^
  55.         $count = 0;
    & \: p, w* `8 Y, ]0 i) e$ g, f, A: `9 x
  56.         if ($res) {
    + N  z4 ]+ v' d. A
  57.             $count = $res[0]->n;9 P1 I4 e9 T% Y
  58.         }
    ) t$ E: ]3 |) Z, V0 F$ n6 z
  59. ( N" W' E* g0 l! b4 h% w
  60.         return $count;
    0 q+ L8 N. w$ V4 h. J4 _' y. _' {
  61.     }& K; H0 a- c: C4 n

  62. 3 @* C# e* i0 ?' {, Y8 ?
  63.     public function update($where = [], $update = [], $upsert = false) {
    % o( B7 m$ V2 t& U3 C
  64.         $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);
    # V0 }5 K& e; l5 `8 b
  65.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);/ R% ~2 o. t4 j! y' s# i

  66.   ]: |; ^) X" v9 I4 h6 t
  67.         return $result->getModifiedCount();! ]0 z9 h+ s: i
  68.     }. k1 P, A' V' c$ ]. E4 j" {
  69. 2 n& P, z* m$ G' A. J, V8 E( a" i
  70.     public function insert($data = []) {5 _! t# z, K9 y* M9 |5 E
  71.         $this->bulk->insert($data);( m' W6 f7 W0 b: X' J$ ~
  72.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    , w" e9 E) W9 f( s% E

  73. / s% e; s8 ^" U' g6 ?
  74.         return $result->getInsertedCount();: m% o0 S: w+ M2 W" |- d2 x- o
  75.     }
    ' ^. k9 M7 F0 w% P; }6 `

  76. # w# X! N9 \. t5 V
  77.     public function delete($where = [], $limit = 1) {
    ) k3 D" P0 [& i" m! v
  78.         $this->bulk->delete($where, ['limit' => $limit]);& ~3 Q# x# ^6 b
  79.         $result = $this->mongodb->executeBulkWrite("$this->database.$this->collection", $this->bulk, $this->writeConcern);
    ( {! D3 X0 Z, I4 l# s/ X1 \! E

  80. 6 u" Q  ?( |  I: J% \" ~
  81.         return $result->getDeletedCount();! h$ T3 l) R$ _' k3 T% V( S
  82.     }
    8 {/ _6 E. [- z/ I4 a5 l
  83. }
复制代码
这样的语法和之前差异太大,改动不方便,换 PHP MongoDB
MongoDB 库1.连接
  • ) }3 ]& C. T# O  e) X
  1. new MongoClient();
复制代码
  • * ?/ O: R) J, l
  1. new MongoDB\Client();
复制代码
2.新增

  • : j2 p5 W  h4 T
  1. $collention->insert($array, $options);
复制代码

  • 1 C3 ]9 K# J  l; V
  1. $resultOne = $collention->insertOne($array, $options);//单
    3 O  G5 ?- D& B1 b5 y5 \7 V7 O% O
  2. $lastId = $resultOne->getInsertedId();6 d, o, |3 H! i" b% e
  3. $resultMany = $collention->insertMany($array, $options);//多% H+ s2 w* R; m* d# X
  4. $count = $resultMany->getInsertedCount();
复制代码
3.修改
  • * n- a/ s; g7 B. W4 @  n" _4 [
  1. $collention->update($condition, [4 r7 k: Y) K9 i$ e
  2.     '$set' => $values
    % @% R6 A& S; p2 y
  3. ,[6 s% V  i3 P. N$ b5 l' @
  4.     'multiple' => true//多条,单条false
    ( `' \' J% j- F( h. ~
  5. ]);
复制代码

  • 3 u! j1 U9 p! R) [* J. u- Z7 ?
  1. $collection->updateOne(
    4 P" w: y) o5 S: Q  C! t1 s; l
  2.     ['state' => 'ny'],0 m6 e/ n) G6 k5 F* E& A
  3.     ['$set' => ['country' => 'us']]
    * D: H$ Q' H: j! D7 O& F
  4. );5 \1 B7 K/ T$ a
  5. $updateResult = $collection->updateMany(1 A$ e# K8 J9 R- M1 f
  6.     ['state' => 'ny'],! M, z3 r) _8 }2 }8 @
  7.     ['$set' => ['country' => 'us']]8 h0 Y% Q7 E* E3 [/ R/ c7 l
  8. );$ z9 |( ]& I4 k# R" ^7 b
  9. $count = $updateResult->getModifiedCount();
复制代码
4.查询

  • 5 _! C0 s/ v( \4 F3 P
  1. $cursor = $collection->find($condition, [
    ' K* Z/ z' Q) R1 a
  2.     'name' => true//指定字段
    . L! u  X9 R+ q' L
  3. ]);
    $ ~& Y' f4 P) ~1 t/ a2 H' ?+ I% N
  4. $cursor->skip(5);
    ! q6 ?' ^$ C8 w! A- p( s. s7 Q6 z* b
  5. $cursor->limit(5);
    % E3 R0 R8 L( t5 m$ Q! x
  6. $cursor->sort([
    6 m  ]* ?9 X# V8 W1 i. j
  7.     'time' => -1- b$ w' [! B, [, j6 S! N
  8. ]);
复制代码

  • ; [5 V3 A1 F; _( t/ T% w9 P
  1. $cursor = $collection->find($condition, [
    7 |2 C( ?; E4 F5 @
  2.     'skip' => 5,' Z0 s& ?% P& K* g  R# D9 L
  3.     'limit' => 5,
    ; m* M# p. `: ?7 |
  4.     'sort' => [
    8 b* G" h% X1 r- }1 |
  5.         'time' => -1- Z* P% d+ `$ Y2 [2 C) l% n
  6.     ],//排序! m! L+ e3 l, w& _8 G
  7.     'projection' => [0 H4 P6 L' D- u0 u0 \
  8.         'name' => 1//指定字段+ t. f% R+ Z. t
  9.     ]4 F* w$ P$ G0 q# ?, `- Q" [# A3 q
  10. ]);
复制代码
5.删除

  • 1 F2 M) e5 Z, ]! K% `+ O
  1. $collention->remove($condition, [" E) Z5 ^* O" n' g1 a  G
  2.     'justOne' => false//删单条' `) i% J/ M- p6 @# H  x
  3. ]);7 d# N% C, k1 D1 ^/ h7 f+ W
  4. $collention->remove([]);//删所有
复制代码

  • 4 w2 \7 [" F+ \& _6 Q& |8 x. u
  1. $result = $collention->deleteOne($condition, $options);* x( T# V5 `3 ?) S' M1 z  m0 u
  2. $collention->deleteMany($condition, $options);7 a2 _/ |' S5 ^6 N, h, J8 ?& j
  3. 1 l$ r6 }2 a" J. E& {# z
  4. $result->getDeletedCount();
复制代码
补充
有些人可能习惯以类似 MySQL 的自增 ID 来处理数据,以前可能使用 findAndModify() 方法来查询并修改:
  1. $collention->findAndModify([
    ( [8 B( C  _3 j4 q, n
  2.     '_id' => $tableName//我在自增表中用其它的表名作主键# j9 j, _' E7 p: E, ~( P2 X
  3. ], [! g0 z1 s' v4 }4 _" r% Z
  4.     '$inc' => ['id' => 1]//自增
    ! u* `' |8 r- D( h
  5. ], [& p7 H2 D4 K& e* d  D* E' m9 }3 p
  6.     '_id' => 0
    1 R  c, Z' h5 s& X' c
  7. ], [7 e1 C/ F3 g* g& K5 ?# T
  8.     'new' => 1//返回修改后的结果,默认是修改前的$ j) p7 u/ [# _' |
  9. ]);
复制代码
现在使用 MongoDB 库的话需要修改为:
  1. $collention->findOneAndUpdate([
    * ]; l# Z( N2 m8 c8 ~+ N: ~
  2.     '_id' => $tableName9 Q8 `1 h$ O; E: F
  3. ], [9 J: z9 }! L8 J. S
  4.     '$inc' => ['id' => 1]* h, K* x* z# Z" T% S: R6 z  N
  5. ], [6 q0 j; v0 H: z: C
  6.     'projection' => ['id' => 1],
    : s% S: B  K2 N. w( T8 \1 w" N
  7.     'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    0 [! R$ {. I$ n
  8. ]);
复制代码
3 }( H% h- s! r. ?
4 X. }& T0 u: l, S
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 19:57 , Processed in 0.064633 second(s), 19 queries .

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