查询操作
2 Q2 `- ^2 D% Z0 Z0 \# Z d8 m U- $filter["season_id"] = 106;2 N+ ^ |" T& w m' s
- //$filter["array.8"] = 'cml123';! j' o; N( \; c p8 l
- * s1 A! Y" r/ C0 K: O j
- $filter = ["matches.events_id"=>1];1 x7 L( |) e$ E5 p2 M' e: P5 |
0 l. U8 x' j# A$ }, [! F- $filter = ["matches.events_id"=>['$in'=>[2,3,4,5,7]]];% W; U+ S% t" u2 o
- $filter = ["matches.events_id"=>['$lt'=>'8'];
/ _) L7 F4 y5 v) V: e
+ `3 Y8 z1 R; F r. w, _5 B- ; ^ I9 O1 G. G! d/ P
- 以上条件说明 http://bbs.cncml.com/forum.php?m ... &extra=page%3D1! d: x4 W0 b3 Z! ^% h/ p# M
4 x( k. S' v$ G/ u2 F6 U# H! X- $options = [
# z3 z9 H( p) v; T - 'projection' => ['_id' => 0,"s_lastid" => 1],
6 D6 ?. b: _2 B( f - 'limit' => 1, //显示条数
. H! V8 o3 M1 T7 k - 'skip' => 1 //跳过几条
: t$ Z# B! m: v, w- o' p* g9 t( O - ];
3 V' y! V8 A, e1 e8 _ - . G! [: l6 K" N! _ ?1 ^" E
- $querys = new MongoDB\Driver\Query($filter,$options);
0 ~% {. {, R) J1 p - $cursors = $manager->executeQuery('football.football_Competition_season_matches', $querys);. Z+ e z3 J& c5 a6 a
- $schedule= mg_querys($cursors);
, e+ D+ c( ~- H$ ]) G0 _, [ - print_r($schedule);
9 B" L$ a% R/ [( C- t" Q
复制代码
1 f" H0 g: G, f) y$ r
, ~8 V, U5 B" f+ {* L! M
2 S" [/ a, i4 v/ x: H& Z# V' H4 y8 }( n( S) D0 x [
------------------------------------------------------------------------------------- 一、更新前通过控制台查看数据,查看命令如下 db.sites.find().pretty() 二、通过php实现数据更新,代码如下 <?php // 1.创建数据库连接对象 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
3 B- Y: }; k4 ~" o0 ~: ?// 2.创建数据更新对象 $bulk = new MongoDB\Driver\BulkWrite; // 说明:更新index=2的数据,把原来的url更新为现在的内容,multi:只更新匹配到的第一条数据 $bulk->update( ['index' => 2], ['$set' => ['url' => 'https://www.java.com']], ['multi' => false, 'upsert' => false] ); 0 Y' p+ l% R: e4 m7 U
// 3.创建更新操作级别对象 // 说明:MongoDB\Driver\WriteConcern::MAJORITY :抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作 // 1000:等待超时时间 $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
: r7 y8 m' o6 l6 R" v, h8 U// 4.执行写命令 // 说明:test.sites是test库下的sites集合 // $bulk:更新对象 // $writeConcern:写操作保证级别 $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern); 9 V4 B: P% K/ t6 s+ |# S9 w
// 5.输出更新后的结果 var_dump($result); 代码截图如下: 运行结果截图如下: 三、通过控制台查看更新后的数据,查看命令如下 db.sites.find().pretty() 6 M* F; V; H/ n9 e7 o1 B
& h& @- s) y$ S% A7 {
|