Skip to content

Commit 65592db

Browse files
committed
DOCSP-41974: Specify a query
1 parent 2419c76 commit 65592db

File tree

5 files changed

+384
-0
lines changed

5 files changed

+384
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ php-library = "MongoDB PHP Library"
2424

2525
[constants]
2626
php-library = "MongoDB PHP Library"
27+
mdb-server = "MongoDB Server"
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
8+
// start-setup
9+
$uri = "<connection string>";
10+
$client = new Client($uri);
11+
12+
$db = $client->db;
13+
$collection = $db->fruits;
14+
15+
// Inserts documents representing fruits
16+
$fruits = [
17+
[
18+
'_id' => 1,
19+
'name' => 'apples',
20+
'qty' => 5,
21+
'rating' => 3,
22+
'color' => 'red',
23+
'type' => ['fuji', 'honeycrisp']
24+
],
25+
[
26+
'_id' => 2,
27+
'name' => 'bananas',
28+
'qty' => 7,
29+
'rating' => 4,
30+
'color' => 'yellow',
31+
'type' => ['cavendish']
32+
],
33+
[
34+
'_id' => 3,
35+
'name' => 'oranges',
36+
'qty' => 6,
37+
'rating' => 2,
38+
'type' => ['naval', 'mandarin']
39+
],
40+
[
41+
'_id' => 4,
42+
'name' => 'pineapples',
43+
'qty' => 3,
44+
'rating' => 5,
45+
'color' => 'yellow'
46+
]
47+
];
48+
49+
$result = $collection->insertMany($fruits);
50+
// end-setup
51+
52+
// Retrieves documents in which the "color" value is "yellow"
53+
// start-find-exact
54+
$cursor = $collection->find(['color' => 'yellow']);
55+
foreach ($cursor as $doc) {
56+
echo json_encode($doc) . PHP_EOL;
57+
}
58+
// end-find-exact
59+
60+
// Retrieves all documents in the collection
61+
// start-find-all
62+
$cursor = $collection->find([]);
63+
foreach ($cursor as $doc) {
64+
echo json_encode($doc) . PHP_EOL;
65+
}
66+
// end-find-all
67+
68+
// Retrieves and prints documents in which the "rating" value is greater than 2
69+
// start-find-comparison
70+
$cursor = $collection->find(['rating' => ['$gt' => 2]]);
71+
foreach ($cursor as $doc) {
72+
echo json_encode($doc) . PHP_EOL;
73+
}
74+
// end-find-comparison
75+
76+
// Retrieves and prints documents that match one or both query filters
77+
// start-find-logical
78+
$cursor = $collection->find([
79+
'$or' => [
80+
['qty' => ['$gt' => 5]],
81+
['color' => 'yellow']
82+
]
83+
]);
84+
foreach ($cursor as $doc) {
85+
echo json_encode($doc) . PHP_EOL;
86+
}
87+
// end-find-logical
88+
89+
// Retrieves and prints documents in which the "type" array has 2 elements
90+
// start-find-array
91+
$cursor = $collection->find(['type' => ['$size' => 2]]);
92+
foreach ($cursor as $doc) {
93+
echo json_encode($doc) . PHP_EOL;
94+
}
95+
// end-find-array
96+
97+
// Retrieves and prints documents that have a "color" field
98+
// start-find-element
99+
$cursor = $collection->find(['color' => ['$exists' => true]]);
100+
foreach ($cursor as $doc) {
101+
echo json_encode($doc) . PHP_EOL;
102+
}
103+
// end-find-element
104+
105+
// Retrieves and prints documents in which the "name" value has at least two consecutive "p" characters
106+
// start-find-evaluation
107+
$cursor = $collection->find(['name' => ['$regex' => 'p{2,}']]);
108+
foreach ($cursor as $doc) {
109+
echo json_encode($doc) . PHP_EOL;
110+
}
111+
// end-find-evaluation
112+
113+
?>

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ MongoDB PHP Library
1212

1313
Installation </tutorial/install-php-library>
1414
Get Started </get-started>
15+
/read
1516
/tutorial
1617
/upgrade
1718
/reference

source/read.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. _php-read:
2+
3+
======================
4+
Read Data from MongoDB
5+
======================
6+
7+
.. toctree::
8+
:titlesonly:
9+
:maxdepth: 1
10+
11+
/read/specify-a-query

0 commit comments

Comments
 (0)