Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
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
PSDI Army
yii2
Commits
c294e033
Commit
c294e033
authored
Dec 02, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit test for "yii\mongo\Collection::mapReduce()" added.
parent
0e082c17
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
2 deletions
+43
-2
Collection.php
extensions/mongo/Collection.php
+15
-2
CollectionTest.php
tests/unit/extensions/mongo/CollectionTest.php
+28
-0
No files found.
extensions/mongo/Collection.php
View file @
c294e033
...
@@ -216,7 +216,9 @@ class Collection extends Object
...
@@ -216,7 +216,9 @@ class Collection extends Object
/**
/**
* Performs aggregation using Mongo Map Reduce mechanism.
* Performs aggregation using Mongo Map Reduce mechanism.
* @param mixed $keys
* @param mixed $keys fields to group by. If an array or non-code object is passed,
* it will be the key used to group results. If instance of [[\MongoCode]] passed,
* it will be treated as a function that returns the key to group by.
* @param array $initial Initial value of the aggregation counter object.
* @param array $initial Initial value of the aggregation counter object.
* @param \MongoCode|string $reduce function that takes two arguments (the current
* @param \MongoCode|string $reduce function that takes two arguments (the current
* document and the aggregation to this point) and does the aggregation.
* document and the aggregation to this point) and does the aggregation.
...
@@ -225,6 +227,7 @@ class Collection extends Object
...
@@ -225,6 +227,7 @@ class Collection extends Object
* - condition - criteria for including a document in the aggregation.
* - condition - criteria for including a document in the aggregation.
* - finalize - function called once per unique key that takes the final output of the reduce function.
* - finalize - function called once per unique key that takes the final output of the reduce function.
* @return array the result of the aggregation.
* @return array the result of the aggregation.
* @see http://docs.mongodb.org/manual/core/map-reduce/
*/
*/
public
function
mapReduce
(
$keys
,
$initial
,
$reduce
,
$options
=
[])
public
function
mapReduce
(
$keys
,
$initial
,
$reduce
,
$options
=
[])
{
{
...
@@ -239,7 +242,17 @@ class Collection extends Object
...
@@ -239,7 +242,17 @@ class Collection extends Object
$options
[
'finalize'
]
=
new
\MongoCode
((
string
)
$options
[
'finalize'
]);
$options
[
'finalize'
]
=
new
\MongoCode
((
string
)
$options
[
'finalize'
]);
}
}
}
}
return
$this
->
mongoCollection
->
group
(
$keys
,
$initial
,
$reduce
,
$options
);
// Avoid possible E_DEPRECATED for $options:
if
(
empty
(
$options
))
{
$result
=
$this
->
mongoCollection
->
group
(
$keys
,
$initial
,
$reduce
);
}
else
{
$result
=
$this
->
mongoCollection
->
group
(
$keys
,
$initial
,
$reduce
,
$options
);
}
if
(
array_key_exists
(
'retval'
,
$result
))
{
return
$result
[
'retval'
];
}
else
{
return
[];
}
}
}
/**
/**
...
...
tests/unit/extensions/mongo/CollectionTest.php
View file @
c294e033
...
@@ -145,4 +145,31 @@ class CollectionTest extends MongoTestCase
...
@@ -145,4 +145,31 @@ class CollectionTest extends MongoTestCase
list
(
$row
)
=
$collection
->
findAll
();
list
(
$row
)
=
$collection
->
findAll
();
$this
->
assertEquals
(
$newData
[
'name'
],
$row
[
'name'
]);
$this
->
assertEquals
(
$newData
[
'name'
],
$row
[
'name'
]);
}
}
/**
* @depends testBatchInsert
*/
public
function
testMapReduce
()
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$rows
=
[
[
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
],
[
'name'
=>
'customer 2'
,
'address'
=>
'customer 2 address'
,
],
];
$collection
->
batchInsert
(
$rows
);
$keys
=
[
'address'
=>
1
];
$initial
=
[
'items'
=>
[]];
$reduce
=
"function (obj, prev) { prev.items.push(obj.name); }"
;
$result
=
$collection
->
mapReduce
(
$keys
,
$initial
,
$reduce
);
$this
->
assertEquals
(
2
,
count
(
$result
));
$this
->
assertNotEmpty
(
$result
[
0
][
'address'
]);
$this
->
assertNotEmpty
(
$result
[
0
][
'items'
]);
}
}
}
\ No newline at end of file
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