Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
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
laravel-mongodb
Commits
6bb72e8b
Commit
6bb72e8b
authored
Aug 27, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #29 from andrewryno/subdocument-aggregation
Modify the aggregation logic to handle keys for subdocuments.
parents
bc686c93
85bad948
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
14 deletions
+29
-14
Builder.php
src/Jenssegers/Mongodb/Builder.php
+15
-5
QueryTest.php
tests/QueryTest.php
+14
-9
No files found.
src/Jenssegers/Mongodb/Builder.php
View file @
6bb72e8b
...
@@ -114,7 +114,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -114,7 +114,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Pass other functions directly
// Pass other functions directly
else
else
{
{
$group
[
$column
]
=
array
(
'$'
.
$function
=>
'$'
.
$column
);
// Normally this aggregate function would overwrite the
// $last group set above, but since we are modifying
// the string, we need to unset it directly.
if
(
isset
(
$group
[
$column
]))
{
unset
(
$group
[
$column
]);
}
$key
=
str_replace
(
'.'
,
'_'
,
$column
);
$group
[
$key
]
=
array
(
'$'
.
$function
=>
'$'
.
$column
);
}
}
}
}
}
}
...
@@ -203,7 +212,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -203,7 +212,8 @@ class Builder extends \Illuminate\Database\Query\Builder {
if
(
isset
(
$results
[
0
]))
if
(
isset
(
$results
[
0
]))
{
{
return
$results
[
0
][
$columns
[
0
]];
$key
=
str_replace
(
'.'
,
'_'
,
$columns
[
0
]);
return
$results
[
0
][
$key
];
}
}
}
}
...
@@ -270,7 +280,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -270,7 +280,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
{
{
// As soon as we find a value that is not an array we assume the user is
// As soon as we find a value that is not an array we assume the user is
// inserting a single document.
// inserting a single document.
if
(
!
is_array
(
$value
))
if
(
!
is_array
(
$value
))
{
{
$batch
=
false
;
break
;
$batch
=
false
;
break
;
}
}
...
@@ -522,7 +532,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -522,7 +532,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
/**
/**
* Convert a key to MongoID if needed
* Convert a key to MongoID if needed
*
*
* @param mixed $id
* @param mixed $id
* @return mixed
* @return mixed
*/
*/
...
@@ -548,7 +558,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -548,7 +558,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
// The new list of compiled wheres
// The new list of compiled wheres
$wheres
=
array
();
$wheres
=
array
();
foreach
(
$this
->
wheres
as
$i
=>
&
$where
)
foreach
(
$this
->
wheres
as
$i
=>
&
$where
)
{
{
// Convert id's
// Convert id's
if
(
isset
(
$where
[
'column'
])
&&
$where
[
'column'
]
==
'_id'
)
if
(
isset
(
$where
[
'column'
])
&&
$where
[
'column'
]
==
'_id'
)
...
...
tests/QueryTest.php
View file @
6bb72e8b
...
@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -5,14 +5,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public
static
function
setUpBeforeClass
()
public
static
function
setUpBeforeClass
()
{
{
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'age'
=>
35
,
'title'
=>
'admin'
,
'subdocument'
=>
array
(
'age'
=>
35
)
));
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
33
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
33
,
'title'
=>
'admin'
,
'subdocument'
=>
array
(
'age'
=>
33
)
));
User
::
create
(
array
(
'name'
=>
'Harry Hoe'
,
'age'
=>
13
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Harry Hoe'
,
'age'
=>
13
,
'title'
=>
'user'
,
'subdocument'
=>
array
(
'age'
=>
13
)
));
User
::
create
(
array
(
'name'
=>
'Robert Roe'
,
'age'
=>
37
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Robert Roe'
,
'age'
=>
37
,
'title'
=>
'user'
,
'subdocument'
=>
array
(
'age'
=>
37
)
));
User
::
create
(
array
(
'name'
=>
'Mark Moe'
,
'age'
=>
23
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Mark Moe'
,
'age'
=>
23
,
'title'
=>
'user'
,
'subdocument'
=>
array
(
'age'
=>
23
)
));
User
::
create
(
array
(
'name'
=>
'Brett Boe'
,
'age'
=>
35
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Brett Boe'
,
'age'
=>
35
,
'title'
=>
'user'
,
'subdocument'
=>
array
(
'age'
=>
35
)
));
User
::
create
(
array
(
'name'
=>
'Tommy Toe'
,
'age'
=>
33
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Tommy Toe'
,
'age'
=>
33
,
'title'
=>
'user'
,
'subdocument'
=>
array
(
'age'
=>
33
)
));
User
::
create
(
array
(
'name'
=>
'Yvonne Yoe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Yvonne Yoe'
,
'age'
=>
35
,
'title'
=>
'admin'
,
'subdocument'
=>
array
(
'age'
=>
35
)
));
User
::
create
(
array
(
'name'
=>
'Error'
,
'age'
=>
null
,
'title'
=>
null
));
User
::
create
(
array
(
'name'
=>
'Error'
,
'age'
=>
null
,
'title'
=>
null
));
}
}
...
@@ -176,9 +176,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -176,9 +176,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
30.5
,
User
::
avg
(
'age'
));
$this
->
assertEquals
(
30.5
,
User
::
avg
(
'age'
));
$this
->
assertEquals
(
244
,
User
::
sum
(
'age'
));
$this
->
assertEquals
(
244
,
User
::
sum
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
max
(
'subdocument.age'
));
$this
->
assertEquals
(
13
,
User
::
min
(
'subdocument.age'
));
$this
->
assertEquals
(
30.5
,
User
::
avg
(
'subdocument.age'
));
$this
->
assertEquals
(
244
,
User
::
sum
(
'subdocument.age'
));
$this
->
assertEquals
(
35
,
User
::
where
(
'title'
,
'admin'
)
->
max
(
'age'
));
$this
->
assertEquals
(
35
,
User
::
where
(
'title'
,
'admin'
)
->
max
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
where
(
'title'
,
'user'
)
->
max
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
where
(
'title'
,
'user'
)
->
max
(
'age'
));
$this
->
assertEquals
(
33
,
User
::
where
(
'title'
,
'admin'
)
->
min
(
'age'
));
$this
->
assertEquals
(
33
,
User
::
where
(
'title'
,
'admin'
)
->
min
(
'age'
));
$this
->
assertEquals
(
13
,
User
::
where
(
'title'
,
'user'
)
->
min
(
'age'
));
$this
->
assertEquals
(
13
,
User
::
where
(
'title'
,
'user'
)
->
min
(
'age'
));
}
}
...
...
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