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
83dcc995
Commit
83dcc995
authored
Mar 28, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-326: Aggregation, runCommand, and index management examples for Docs
parent
7417ecf1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
160 additions
and
2 deletions
+160
-2
DocumentationExamplesTest.php
tests/DocumentationExamplesTest.php
+160
-2
No files found.
tests/DocumentationExamplesTest.php
View file @
83dcc995
...
@@ -851,8 +851,6 @@ class DocumentationExamplesTest extends FunctionalTestCase
...
@@ -851,8 +851,6 @@ class DocumentationExamplesTest extends FunctionalTestCase
$this
->
assertCursorCount
(
1
,
$cursor
);
$this
->
assertCursorCount
(
1
,
$cursor
);
}
}
public
function
testExample_55_58
()
public
function
testExample_55_58
()
{
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
...
@@ -1022,6 +1020,166 @@ class DocumentationExamplesTest extends FunctionalTestCase
...
@@ -1022,6 +1020,166 @@ class DocumentationExamplesTest extends FunctionalTestCase
$this
->
assertNull
(
$nextChange
);
$this
->
assertNull
(
$nextChange
);
}
}
public
function
testAggregation_example_1
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Aggregation Example 1
$cursor
=
$db
->
sales
->
aggregate
([
[
'$match'
=>
[
'items.fruit'
=>
'banana'
]],
[
'$sort'
=>
[
'date'
=>
1
]],
]);
// End Aggregation Example 1
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testAggregation_example_2
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Aggregation Example 2
$cursor
=
$db
->
sales
->
aggregate
([
[
'$unwind'
=>
'$items'
],
[
'$match'
=>
[
'items.fruit'
=>
'banana'
]],
[
'$group'
=>
[
'_id'
=>
[
'day'
=>
[
'$dayOfWeek'
=>
'$date'
]],
'count'
=>
[
'$sum'
=>
'$items.quantity'
]],
],
[
'$project'
=>
[
'dayOfWeek'
=>
'$_id.day'
,
'numberSold'
=>
'$count'
,
'_id'
=>
0
,
]
],
[
'$sort'
=>
[
'numberSold'
=>
1
]],
]);
// End Aggregation Example 2
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testAggregation_example_3
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Aggregation Example 3
$cursor
=
$db
->
sales
->
aggregate
([
[
'$unwind'
=>
'$items'
],
[
'$group'
=>
[
'_id'
=>
[
'day'
=>
[
'$dayOfWeek'
=>
'$date'
]],
'items_sold'
=>
[
'$sum'
=>
'$items.quantity'
],
'revenue'
=>
[
'$sum'
=>
[
'$multiply'
=>
[
'$items.quantity'
,
'$items.price'
]
]
],
]],
[
'$project'
=>
[
'day'
=>
'$_id.day'
,
'revenue'
=>
1
,
'items_sold'
=>
1
,
'discount'
=>
[
'$cond'
=>
[
'if'
=>
[
'$lte'
=>
[
'$revenue'
,
250
]],
'then'
=>
25
,
'else'
=>
0
,
]
],
]],
]);
// End Aggregation Example 3
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testAggregation_example_4
()
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.6.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'$lookup does not support "let" option'
);
}
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Aggregation Example 4
$cursor
=
$db
->
air_alliances
->
aggregate
([
[
'$lookup'
=>
[
'from'
=>
'air_airlines'
,
'let'
=>
[
'constituents'
=>
'$airlines'
],
'pipeline'
=>
[[
'$match'
=>
[
'$expr'
=>
[
'$in'
=>
[
'$name'
,
'$constituents'
]]
]]],
'as'
=>
'airlines'
,
]],
[
'$project'
=>
[
'_id'
=>
0
,
'name'
=>
1
,
'airlines'
=>
[
'$filter'
=>
[
'input'
=>
'$airlines'
,
'as'
=>
'airline'
,
'cond'
=>
[
'$eq'
=>
[
'$$airline.country'
,
'Canada'
]],
]
],
]],
]);
// End Aggregation Example 4
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testRunCommand_example_1
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start runCommand Example 1
$cursor
=
$db
->
command
([
'buildInfo'
=>
1
]);
$result
=
$cursor
->
toArray
()[
0
];
// End runCommand Example 1
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testRunCommand_example_2
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
$db
->
dropCollection
(
'restaurants'
);
$db
->
createCollection
(
'restaurants'
);
// Start runCommand Example 2
$cursor
=
$db
->
command
([
'collStats'
=>
'restaurants'
]);
$result
=
$cursor
->
toArray
()[
0
];
// End runCommand Example 2
$this
->
assertInstanceOf
(
'MongoDB\Driver\Cursor'
,
$cursor
);
}
public
function
testIndex_example_1
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Index Example 1
$indexName
=
$db
->
records
->
createIndex
([
'score'
=>
1
]);
// End Index Example 1
$this
->
assertEquals
(
'score_1'
,
$indexName
);
}
public
function
testIndex_example_2
()
{
$db
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
());
// Start Index Example 2
$indexName
=
$db
->
restaurants
->
createIndex
(
[
'cuisine'
=>
1
,
'name'
=>
1
],
[
'partialFilterExpression'
=>
[
'rating'
=>
[
'$gt'
=>
5
]]]
);
// End Index Example 2
$this
->
assertEquals
(
'cuisine_1_name_1'
,
$indexName
);
}
/**
/**
* Return the test collection name.
* Return the test collection name.
*
*
...
...
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