cncml手绘网
标题: PHP7连接MongoDB进行数据更新 [打印本页]
作者: admin 时间: 2019-3-19 00:38
标题: PHP7连接MongoDB进行数据更新
查询操作
. k7 S" @7 S' |$ ?' L
- $filter["season_id"] = 106;
# ~* u3 M0 _; [& D/ i% b% j - //$filter["array.8"] = 'cml123';
; n* n q/ ]) Y - ! ]% W) z" b: m6 I! o
- $filter = ["matches.events_id"=>1];: L+ P$ b0 {+ I# i/ R
3 p. Q0 }( w8 K# z: \6 u! v- $filter = ["matches.events_id"=>['$in'=>[2,3,4,5,7]]]; K9 d7 u5 u" I9 W/ n
- $filter = ["matches.events_id"=>['$lt'=>'8'];
' m8 L( c) ~7 T4 J- @# C* `: z
2 j) n' O- Z1 Q- h. J" b: r# j
7 U' F8 |. f; c- 以上条件说明 http://bbs.cncml.com/forum.php?m ... &extra=page%3D1( J: D) Z8 ^4 f: ^# v
- l3 v1 y3 i! t, k- $options = [: c! _$ A/ Y5 }6 F9 z2 U9 L% |
- 'projection' => ['_id' => 0,"s_lastid" => 1],6 C0 s8 _' X, Y* e
- 'limit' => 1, //显示条数% A) Q) B7 {- G. q
- 'skip' => 1 //跳过几条2 c, v# C; M, l6 g6 L' E7 M# b& Q
- ];* K( I* f9 p7 n& o5 B- s) D
- + s1 o% O6 M$ B& a
- $querys = new MongoDB\Driver\Query($filter,$options);
! i7 K* S7 A0 ]2 o# y - $cursors = $manager->executeQuery('football.football_Competition_season_matches', $querys);1 m5 x. D0 j" }2 `0 U7 d! U# `
- $schedule= mg_querys($cursors);7 A' A. @6 w4 V- A8 k
- print_r($schedule);
% ~# M4 H0 s3 d+ p8 v
复制代码 5 K/ h. I8 i$ l' d
' }. T" h# z, I# ?% p* M+ ?2 V7 X4 g
. M( A, V# M& [# j, k u
4 L; q, p6 {0 k% x @, {7 m8 G, t-------------------------------------------------------------------------------------
一、更新前通过控制台查看数据,查看命令如下
db.sites.find().pretty()
二、通过php实现数据更新,代码如下
<?php
// 1.创建数据库连接对象
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
& H0 K% t/ z0 Q6 g9 M// 2.创建数据更新对象
$bulk = new MongoDB\Driver\BulkWrite;
// 说明:更新index=2的数据,把原来的url更新为现在的内容,multi:只更新匹配到的第一条数据
$bulk->update(
['index' => 2],
['$set' => ['url' => 'https://www.java.com']],
['multi' => false, 'upsert' => false]
);
2 Q) p8 E: k- f' a
// 3.创建更新操作级别对象
// 说明:MongoDB\Driver\WriteConcern::MAJORITY :抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作
// 1000:等待超时时间
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
5 i; G" ~. {" I8 W, ~$ Q
// 4.执行写命令
// 说明:test.sites是test库下的sites集合
// $bulk:更新对象
// $writeConcern:写操作保证级别
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
6 D9 e \4 {! K8 ^5 A4 e4 ?
// 5.输出更新后的结果
var_dump($result);
代码截图如下:
运行结果截图如下:
三、通过控制台查看更新后的数据,查看命令如下
db.sites.find().pretty()
0 m$ o7 g7 u' R, J
. I- W, \( r( y- s
| 欢迎光临 cncml手绘网 (http://bbs.cncml.com/) |
Powered by Discuz! X3.2 |