2
2
3
3
require 'vendor/autoload.php ' ;
4
4
5
- $ client = new MongoDB \Client ('<connection string> ' );
5
+ $ uri = getenv ('MONGODB_URI ' ) ?: throw new RuntimeException ('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset ' );
6
+ $ client = new MongoDB \Client ($ uri );
6
7
7
8
// start-db-coll
8
- $ db = $ client ->sample_restaurants ;
9
- $ collection = $ db ->restaurants ;
9
+ $ collection = $ client ->sample_restaurants ->restaurants ;
10
10
// end-db-coll
11
11
12
12
// Retrieves 5 documents that have a "cuisine" value of "Italian"
13
13
// start-limit
14
- $ options = [
15
- ' limit ' => 5 ,
16
- ];
17
- $ cursor = $ collection -> find ([ ' cuisine ' => ' Italian ' ], $ options );
14
+ $ cursor = $ collection -> find (
15
+ [ ' cuisine ' => ' Italian ' ] ,
16
+ [ ' limit ' => 5 ]
17
+ );
18
18
19
19
foreach ($ cursor as $ doc ) {
20
20
echo json_encode ($ doc ) . PHP_EOL ;
23
23
24
24
// Retrieves documents with a "cuisine" value of "Italian" and sorts in ascending "name" order
25
25
// start-sort
26
- $ options = [
27
- 'sort ' => ['name ' => 1 ],
28
- ];
26
+ $ cursor = $ collection ->find (
27
+ ['cuisine ' => 'Italian ' ],
28
+ ['sort ' => ['name ' => 1 ]]
29
+ );
29
30
30
- $ cursor = $ collection ->find (['cuisine ' => 'Italian ' ], $ options );
31
31
foreach ($ cursor as $ doc ) {
32
32
echo json_encode ($ doc ) . PHP_EOL ;
33
33
}
34
34
// end-sort
35
35
36
36
// Retrieves documents with a "borough" value of "Manhattan" but skips the first 10 results
37
37
// start-skip
38
- $ options = [
39
- 'skip ' => 10 ,
40
- ];
38
+ $ cursor = $ collection ->find (
39
+ ['borough ' => 'Manhattan ' ],
40
+ ['skip ' => 10 ]
41
+ );
41
42
42
- $ cursor = $ collection ->find (['borough ' => 'Manhattan ' ], $ options );
43
43
foreach ($ cursor as $ doc ) {
44
44
echo json_encode ($ doc ) . PHP_EOL ;
45
45
}
59
59
echo json_encode ($ doc ) . PHP_EOL ;
60
60
}
61
61
// end-limit-sort-skip
62
-
63
- ?>
0 commit comments