Skip to content

Commit 4dd9f25

Browse files
committed
edits
1 parent 1a32205 commit 4dd9f25

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

source/includes/read/distinct.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
<?php
22
require 'vendor/autoload.php';
33

4+
use MongoDB\BSON\Document;
5+
46
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
57
$client = new MongoDB\Client($uri);
68

79
// start-db-coll
8-
$db = $client->sample_training;
9-
$collection = $db->companies;
10+
$collection = $client->sample_restaurants->restaurants;
1011
// end-db-coll
1112

12-
// Finds one document with a "name" value of "LinkedIn"
13-
// start-find-one
14-
$document = $collection->findOne(['name' => 'LinkedIn']);
15-
echo json_encode($document) . "\n";
16-
// end-find-one
17-
18-
// Finds documents with a "founded_year" value of 1970
19-
// start-find-many
20-
$results = $collection->find(['founded_year' => 1970]);
21-
// end-find-many
22-
23-
// Prints documents with a "founded_year" value of 1970
24-
// start-cursor
25-
foreach ($results as $doc) {
26-
echo json_encode($doc) . "\n";
13+
// Retrieves distinct values of the "borough" field
14+
// start-distinct
15+
$results = $collection->distinct('borough', []);
16+
foreach ($results as $value) {
17+
echo json_encode($value) . PHP_EOL;
18+
}
19+
// end-distinct
20+
21+
// Retrieves distinct "borough" field values for documents with a "cuisine" value of "Italian"
22+
// start-distinct-with-query
23+
$results = $collection->distinct('borough', ['cuisine' => 'Italian']);
24+
foreach ($results as $value) {
25+
echo json_encode($value) . PHP_EOL;
2726
}
28-
// end-cursor
27+
// end-distinct-with-query
2928

30-
// Finds and prints up to 5 documents with a "number_of_employees" value of 1000
31-
// start-modify
32-
$results = $collection->find(
33-
['number_of_employees' => 1000],
34-
['limit' => 5]
35-
);
29+
// Retrieves distinct "name" field values for documents matching the "borough" and "cuisine" fields query
30+
// and attaches a comment to the operation
31+
// start-distinct-with-comment
32+
$options = ['comment' => 'Bronx pizza restaurants'];
33+
$query = ['$and' => [['borough' => 'Bronx'], ['cuisine' => 'Pizza']]];
34+
$results = $collection->distinct('name', $query, $options);
3635

37-
foreach ($results as $doc) {
38-
echo json_encode($doc) . "\n";
36+
foreach ($results as $value) {
37+
echo json_encode($value) . PHP_EOL;
3938
}
40-
// end-modify
39+
// end-distinct-with-comment
40+

source/read.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Read Data from MongoDB
99
:maxdepth: 1
1010

1111
/read/retrieve
12-
/read/distint
12+
/read/distinct

source/read/distinct.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ distinct values of a specified field across a collection.
2525

2626
Within a collection, different documents might contain different values for a
2727
single field. For example, one document in a ``restaurants`` collection has a
28-
``borough`` value of ``"Manhattan"``, and another has a ``borough`` value of
29-
``"Queens"``. By using the {+php-library+}, you can retrieve all the unique values
28+
``borough`` value of ``'Manhattan'``, and another has a ``borough`` value of
29+
``'Queens'``. By using the {+php-library+}, you can retrieve all the unique values
3030
that a field contains across multiple documents in a collection.
3131

3232
Sample Data
@@ -59,6 +59,7 @@ The following example retrieves the distinct values of the ``borough`` field in
5959
the ``restaurants`` collection:
6060

6161
.. io-code-block::
62+
:copyable:
6263

6364
.. input:: /includes/read/distinct.php
6465
:start-after: start-distinct
@@ -67,10 +68,13 @@ the ``restaurants`` collection:
6768
:dedent:
6869

6970
.. output::
71+
:visible: false
7072

71-
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Missing", "Queens", "Staten Island" ],
72-
"ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { ... } },
73-
"signature" : { "hash" : { ... }, "keyId" : ... } }, "operationTime" : { "$timestamp" : { ... } }
73+
"Bronx"
74+
"Manhattan"
75+
"Missing"
76+
"Queens"
77+
"Staten Island"
7478

7579
The operation returns an array that stores each distinct ``borough`` field value. Although
7680
several documents have the same value in the ``borough`` field, each value appears in the
@@ -88,6 +92,7 @@ The following example retrieves the distinct values of the ``borough`` field for
8892
all documents that have a ``cuisine`` field value of ``'Italian'``:
8993

9094
.. io-code-block::
95+
:copyable:
9196

9297
.. input:: /includes/read/distinct.php
9398
:start-after: start-distinct-with-query
@@ -96,10 +101,12 @@ all documents that have a ``cuisine`` field value of ``'Italian'``:
96101
:dedent:
97102

98103
.. output::
104+
:visible: false
99105

100-
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island" ],
101-
"ok" : 1.0, "$clusterTime" : { "clusterTime" : { "$timestamp" : { ... },
102-
"signature" : { "hash" : { ... }, "keyId" : ... } }, "operationTime" : { "$timestamp" : { ... } }
106+
"Bronx"
107+
"Manhattan"
108+
"Queens"
109+
"Staten Island"
103110

104111
Modify Distinct Behavior
105112
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -138,6 +145,7 @@ all documents that have a ``borough`` field value of ``'Bronx'`` and a
138145
in an options array to add a comment to the operation:
139146

140147
.. io-code-block::
148+
:copyable:
141149

142150
.. input:: /includes/read/distinct.php
143151
:start-after: start-distinct-with-comment
@@ -146,9 +154,16 @@ in an options array to add a comment to the operation:
146154
:dedent:
147155

148156
.. output::
149-
150-
{ "values" : [ "$1.25 Pizza", "18 East Gunhill Pizza", "2 Bros", "Aenos Pizza", "Alitalia Pizza Restaurant", … ],
151-
"ok" : 1.0, "$clusterTime" : { "clusterTime" : { … }, "signature" : { … }, "keyId" : … } }, "operationTime" : { … } }
157+
:visible: false
158+
159+
"$1.25 Pizza"
160+
"18 East Gunhill Pizza"
161+
"2 Bros"
162+
"Aenos Pizza"
163+
"Alitalia Pizza Restaurant"
164+
"Amici Pizza And Pasta"
165+
"Angie'S Cafe Pizza"
166+
...
152167

153168
API Documentation
154169
-----------------

0 commit comments

Comments
 (0)