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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10788|回复: 0
打印 上一主题 下一主题

[php学习资料] PHP7连接MongoDB进行数据更新

[复制链接]
跳转到指定楼层
楼主
发表于 2019-3-19 00:38:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查询操作

# b: d$ _+ p* e2 R5 p* Z9 w' ^  q
  1. $filter["season_id"] = 106;" N3 q' w0 B  j6 R% t
  2. //$filter["array.8"] = 'cml123';7 U; o" E7 \2 F

  3. / a! j2 G- f: p2 Y, ?; \
  4. $filter = ["matches.events_id"=>1];
    4 b0 C( b1 [2 Q( O) K

  5. 9 s$ i8 p' v5 l8 b9 f, O9 P
  6. $filter = ["matches.events_id"=>['$in'=>[2,3,4,5,7]]];% O' X2 W/ G# S% ^* o4 o9 R
  7. $filter = ["matches.events_id"=>['$lt'=>'8'];1 F# X3 c  c/ e' M. s1 \0 f" o- v3 o+ K
  8. ! m, h# s9 }: g6 a! P& R% _* d

  9. : L9 u$ H5 h/ r6 p3 b& G3 j& A2 Y
  10. 以上条件说明 http://bbs.cncml.com/forum.php?m ... &extra=page%3D1
    - n& D4 n4 B" V1 \  [3 {
  11. 2 o8 A& M; @* ]" [- b2 o% i$ M
  12. $options = [
    " `- U# b7 U$ G2 x; Y3 u$ R
  13.     'projection' => ['_id' => 0,"s_lastid" => 1],
    ' J# B  p9 ~7 J, |
  14.     'limit' => 1,       //显示条数
    ) f$ O5 E* q3 y
  15.     'skip' => 1         //跳过几条4 Y' e+ D8 t9 i2 F" [
  16. ];% w/ x* x. G9 O; \0 ^) W6 ^7 J$ d4 t
  17. 7 _. M' n. ?5 ?4 ^* ^& }! X
  18. $querys = new MongoDB\Driver\Query($filter,$options);/ ]3 m9 P9 c$ r* u+ }1 t
  19. $cursors = $manager->executeQuery('football.football_Competition_season_matches', $querys);; Q% g2 W$ B) [2 b
  20. $schedule= mg_querys($cursors);
    % t2 E9 a0 Y+ q
  21. print_r($schedule);
    . V6 D( _$ F- {+ \4 D, O
复制代码
' a9 ?' `# a- Y' }: K

4 V8 B: }& L1 A! A6 a
5 V7 ~& |% x. x  {
6 y* g' A0 S, e/ {
-------------------------------------------------------------------------------------
一、更新前通过控制台查看数据,查看命令如下
    db.sites.find().pretty()
二、通过php实现数据更新,代码如下
<?php
// 1.创建数据库连接对象
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

3 M3 d3 D% P/ {! J8 e
// 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 J* C1 l% C. C2 o3 d# u
// 3.创建更新操作级别对象
// 说明:MongoDB\Driver\WriteConcern::MAJORITY :抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作
// 1000:等待超时时间
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

+ L' b& Z% w: _. _2 K( \6 d- E
// 4.执行写命令
// 说明:test.sites是test库下的sites集合
// $bulk:更新对象
// $writeConcern:写操作保证级别
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
- T8 ?2 t" O( Q3 x- |( G# K
// 5.输出更新后的结果
var_dump($result);
代码截图如下:
运行结果截图如下:
三、通过控制台查看更新后的数据,查看命令如下
    db.sites.find().pretty()
! U; s# k* h" H& r. d

' |. Z# }3 p; M/ a$ L# ~
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-20 04:08 , Processed in 0.166864 second(s), 22 queries .

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