查询操作
& l+ u* V. U( l) |& S$ k; W. a S- $filter["season_id"] = 106;
! O3 J; r( |- K+ G - //$filter["array.8"] = 'cml123';; l3 p" E) X$ f; i( Y! L
- 6 _" B5 k7 l' r! \
- $filter = ["matches.events_id"=>1];9 k7 i( y7 S: ^3 F% _
- * l! {. U2 `: h$ ?5 q5 W$ W8 F
- $filter = ["matches.events_id"=>['$in'=>[2,3,4,5,7]]];6 y5 p3 n% O5 g, l) l2 m$ {0 ^
- $filter = ["matches.events_id"=>['$lt'=>'8'];
8 k. ]3 V9 M7 v' A/ m ?' `0 r
6 _( G; b5 k3 D0 t8 z9 O
! |7 V! g1 S6 @- 以上条件说明 http://bbs.cncml.com/forum.php?m ... &extra=page%3D1! p" ^/ `7 O9 {- D( I* }
- : U5 x6 u+ \% `9 U" a+ m4 U0 {
- $options = [
: w* n q8 h5 ] - 'projection' => ['_id' => 0,"s_lastid" => 1],) N8 I- a b, y' ?$ F9 X! _
- 'limit' => 1, //显示条数
. _' a2 y/ V/ a5 y, B/ c - 'skip' => 1 //跳过几条
2 k: [0 r0 }$ Q& C2 n - ];
7 f4 o2 s1 {9 U
3 U) Q+ b7 l6 b+ D- $querys = new MongoDB\Driver\Query($filter,$options);
! ^+ b' r/ k" S4 M2 R# D - $cursors = $manager->executeQuery('football.football_Competition_season_matches', $querys);
: i, k$ X: r9 f- I( M9 _, T3 c - $schedule= mg_querys($cursors);
! o( g/ F, t1 w; C X - print_r($schedule);
- O1 X2 W' I4 S$ k9 U
复制代码
! Q5 c7 i/ U) ]. p- X , s& `; ~4 O4 r
8 g4 }7 x; T0 e' x. W; ~# t
2 t9 }, U* G( ~7 O------------------------------------------------------------------------------------- 一、更新前通过控制台查看数据,查看命令如下 db.sites.find().pretty() 二、通过php实现数据更新,代码如下 <?php // 1.创建数据库连接对象 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
8 A4 C2 Y. v! H9 G- y// 2.创建数据更新对象 $bulk = new MongoDB\Driver\BulkWrite; // 说明:更新index=2的数据,把原来的url更新为现在的内容,multi:只更新匹配到的第一条数据 $bulk->update( ['index' => 2], ['$set' => ['url' => 'https://www.java.com']], ['multi' => false, 'upsert' => false] );
4 H% g4 y4 M3 @! Z* l: j0 i// 3.创建更新操作级别对象 // 说明:MongoDB\Driver\WriteConcern::MAJORITY :抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作 // 1000:等待超时时间 $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); ; z0 i. C4 h9 e: M. P
// 4.执行写命令 // 说明:test.sites是test库下的sites集合 // $bulk:更新对象 // $writeConcern:写操作保证级别 $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
8 ?2 l/ N9 a8 p// 5.输出更新后的结果 var_dump($result); 代码截图如下: 运行结果截图如下: 三、通过控制台查看更新后的数据,查看命令如下 db.sites.find().pretty() % h; c2 _ v( n2 Z
- C" Z) L7 m/ ]. E8 v8 H% D3 P
|