Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
6ece2bcf
Commit
6ece2bcf
authored
Mar 29, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use listCollections for command cursor example, move aggregate to CRUD
parent
0a3b8270
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
19 deletions
+63
-19
collection.md
docs/classes/collection.md
+1
-0
commands.md
docs/tutorial/commands.md
+17
-19
crud.md
docs/tutorial/crud.md
+45
-0
No files found.
docs/classes/collection.md
View file @
6ece2bcf
...
@@ -131,6 +131,7 @@ useCursor (boolean)
...
@@ -131,6 +131,7 @@ useCursor (boolean)
### See Also
### See Also
*
[
MongoDB Manual: aggregate command
](
http://docs.mongodb.org/manual/reference/command/aggregate/
)
*
[
MongoDB Manual: aggregate command
](
http://docs.mongodb.org/manual/reference/command/aggregate/
)
*
[
MongoDB Manual: Aggregation Pipeline
](
https://docs.mongodb.org/manual/core/aggregation-pipeline/
)
---
---
...
...
docs/tutorial/commands.md
View file @
6ece2bcf
...
@@ -42,12 +42,12 @@ object(MongoDB\Model\BSONDocument)#2 (1) {
...
@@ -42,12 +42,12 @@ object(MongoDB\Model\BSONDocument)#2 (1) {
## Iterable Results as a Command Cursor
## Iterable Results as a Command Cursor
Some commands, such as
[
aggregate
][
aggregate
]
with the "cursor" option, may
Some commands, such as
[
listCollections
][
listcollections
]
, return their results
return their results
via an iterable command cursor. In this case, the returned
via an iterable command cursor. In this case, the returned
[
MongoDB\Driver\Cursor
][
cursor
]
may be iterated in the same manner as one might
[
MongoDB\Driver\Cursor
][
cursor
]
may be iterated in the same manner as one might
do with a
[
Collection::find()
][
find
]
query, like so:
do with a
[
Collection::find()
][
find
]
query, like so:
[
aggregate
]:
http://docs.mongodb.org/manual/reference/command/aggregate
/
[
listcollections
]:
http://docs.mongodb.org/manual/reference/command/listCollections
/
[
find
]:
../classes/collection.md#find
[
find
]:
../classes/collection.md#find
```
```
...
@@ -55,31 +55,29 @@ do with a [Collection::find()][find] query, like so:
...
@@ -55,31 +55,29 @@ do with a [Collection::find()][find] query, like so:
$database = (new MongoDB\Client)->demo;
$database = (new MongoDB\Client)->demo;
$cursor = $database->command([
$cursor = $database->command(['listCollections' => 1]);
'aggregate' => 'zips',
'pipeline' => [
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]],
['$limit' => 5],
],
'cursor' => new \stdClass,
]);
foreach ($cursor as $
state
) {
foreach ($cursor as $
collection
) {
printf("%s has %d zip codes\n", $state['_id'], $state['count'])
;
echo $collection['name'], "\n"
;
}
}
```
```
The above example would output something similar to:
The above example would output something similar to:
```
```
TX has 1671 zip codes
persons
NY has 1595 zip codes
posts
CA has 1516 zip codes
zips
PA has 1458 zip codes
IL has 1237 zip codes
```
```
**Note:**
at the protocol level, commands that support a cursor actually return
a single result document with the essential ingredients for constructing the
command cursor (i.e. the cursor's ID, namespace, and first batch of results);
however, the driver's
[
executeCommand()
][
executecommand
]
method already detects
such a result and constructs the iterable command cursor for us.
[
executecommand
]:
http://php.net/manual/en/mongodb-driver-manager.executecommand.php
## Specifying a Read Preference
## Specifying a Read Preference
Some commands, such as
[
createUser
][
createUser
]
, can only be executed on a
Some commands, such as
[
createUser
][
createUser
]
, can only be executed on a
...
...
docs/tutorial/crud.md
View file @
6ece2bcf
...
@@ -159,3 +159,48 @@ The above example would output something similar to:
...
@@ -159,3 +159,48 @@ The above example would output something similar to:
10025: NEW YORK, NY
10025: NEW YORK, NY
90201: BELL GARDENS, CA
90201: BELL GARDENS, CA
```
```
## Aggregation
The
[
Aggregation Framework
][
aggregation
]
may be used to issue complex queries
that filter, transform, and group collection data. The
[
aggregate()
][
aggregate
]
method returns a
[
Traversable
][
traversable
]
object, which may be iterated
upon to access the results of an aggregation pipeline.
[
aggregation
]:
https://docs.mongodb.org/manual/core/aggregation-pipeline/
[
aggregate
]:
../classes/collection.md#aggregate
[
traversable
]:
http://php.net/traversable
```
<?php
$collection = (new MongoDB\Client)->demo->zips;
$cursor = $collection->aggregate([
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]],
['$limit' => 5],
]);
foreach ($cursor as $state) {
printf("%s has %d zip codes\n", $state['_id'], $state['count']);
}
```
The above example would output something similar to:
```
TX has 1671 zip codes
NY has 1595 zip codes
CA has 1516 zip codes
PA has 1458 zip codes
IL has 1237 zip codes
```
**Note:**
[
aggregate()
][
aggregate
]
is documented as returning a
[
Traversable
][
traversable
]
object because the
[
aggregate
][
aggregate-cmd
]
command
may return its results inline (i.e. a single result document's array field,
which the library will package as a PHP iterator) or via a command cursor (i.e.
[
MongoDB\Driver\Cursor
][
cursor
]
).
[
aggregate-cmd
]:
(http://docs.mongodb.org/manual/reference/command/aggregate/)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment