加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 学习资料

PHP7连接MongoDB进行数据更新

时间:2021-03-21 21:42:54  来源:  作者:
查询操作
 
  1. $filter["season_id"] = 106;
  2. //$filter["array.8"] = 'cml123';
  3.  
  4. $filter = ["matches.events_id"=>1];
  5.  
  6. $filter = ["matches.events_id"=>['$in'=>[2,3,4,5,7]]];
  7. $filter = ["matches.events_id"=>['$lt'=>'8'];
  8.  
  9.  
  10. 以上条件说明 http://bbs.cncml.com/forum.php?m ... &extra=page%3D1
  11.  
  12. $options = [
  13.     'projection' => ['_id' => 0,"s_lastid" => 1],
  14.     'limit' => 1,       //显示条数
  15.     'skip' => 1         //跳过几条
  16. ];
  17.  
  18. $querys = new MongoDB\Driver\Query($filter,$options);
  19. $cursors = $manager->executeQuery('football.football_Competition_season_matches', $querys);
  20. $schedule= mg_querys($cursors);
  21. print_r($schedule);
  22.  
复制代码


 
-------------------------------------------------------------------------------------
一、更新前通过控制台查看数据,查看命令如下
    db.sites.find().pretty()
二、通过php实现数据更新,代码如下
<?php
// 1.创建数据库连接对象
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
 
// 2.创建数据更新对象
$bulk = new MongoDB\Driver\BulkWrite;
// 说明:更新index=2的数据,把原来的url更新为现在的内容,multi:只更新匹配到的第一条数据
$bulk->update(
    ['index' => 2],
    ['$set' => ['url' => 'https://www.java.com']],
    ['multi' => false, 'upsert' => false]
);
 
// 3.创建更新操作级别对象
// 说明:MongoDB\Driver\WriteConcern::MAJORITY :抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作
// 1000:等待超时时间
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
 
// 4.执行写命令
// 说明:test.sites是test库下的sites集合
// $bulk:更新对象
// $writeConcern:写操作保证级别
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
 
// 5.输出更新后的结果
var_dump($result);
代码截图如下:
运行结果截图如下:
三、通过控制台查看更新后的数据,查看命令如下
    db.sites.find().pretty()

 
 
 
 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
实现php间隔一段时间执行一次某段代码
实现php间隔一段时间
相关文章
    无相关信息
栏目更新
栏目热门