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
4089d6e9
Commit
4089d6e9
authored
Apr 30, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding aggregation
parent
17234dac
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
68 deletions
+68
-68
README.md
README.md
+7
-8
Query.php
src/Jenssegers/Mongodb/Query.php
+61
-60
No files found.
README.md
View file @
4089d6e9
...
@@ -89,16 +89,15 @@ Examples
...
@@ -89,16 +89,15 @@ Examples
})
})
->get();
->get();
**Distinct**
Distinct is limited to a single column.
$names = Users::distinct('name')->get();
**Group By**
**Group By**
Grouping does not support sorting and limiting at this moment.
$users = Users::groupBy('title')->get(array('title', 'name'));
**Aggregation**
$users = Users::groupBy('title')->get();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
All basis insert, update, delete and select methods should be implemented. Feel free to fork and help completing this library!
All basis insert, update, delete and select methods should be implemented. Feel free to fork and help completing this library!
\ No newline at end of file
src/Jenssegers/Mongodb/Query.php
View file @
4089d6e9
...
@@ -23,9 +23,6 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -23,9 +23,6 @@ class Query extends \Illuminate\Database\Query\Builder {
'<='
=>
'$lte'
,
'<='
=>
'$lte'
,
'>'
=>
'$gt'
,
'>'
=>
'$gt'
,
'>='
=>
'$gte'
,
'>='
=>
'$gte'
,
'in'
=>
'$in'
,
'exists'
=>
'$exists'
,
'or'
=>
'$or'
,
);
);
/**
/**
...
@@ -73,62 +70,78 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -73,62 +70,78 @@ class Query extends \Illuminate\Database\Query\Builder {
$this
->
columns
=
array
();
$this
->
columns
=
array
();
}
}
// Compile wheres
$wheres
=
$this
->
compileWheres
();
if
(
$this
->
distinct
)
// Use aggregation framework if needed
if
(
$this
->
groups
||
$this
->
aggregate
)
{
{
// We can only return an array of distinct values for a single column
$pipeline
=
array
();
return
$this
->
collection
->
distinct
(
$this
->
distinct
,
$this
->
compileWheres
());
}
// Group
else
if
(
count
(
$this
->
groups
))
$group
=
array
();
{
$group
[
'_id'
]
=
$this
->
groups
?
$this
->
groups
:
0
;
// We can pass our wheres as a condition
$options
=
array
();
// Columns
$options
[
'condition'
]
=
$this
->
compileWheres
();
foreach
(
$this
->
columns
as
$column
)
// Initial value and reduce code
$initial
=
array
(
"item"
=>
""
);
$reduce
=
"function (obj, prev) { prev.item = obj; }"
;
// Get results
$result
=
$this
->
collection
->
group
(
$this
->
groups
,
$initial
,
$reduce
,
$options
);
$items
=
$result
[
'retval'
];
// Transform results to a nice array
$results
=
array
();
foreach
(
$items
as
$item
)
{
{
$
results
[]
=
$item
[
'item'
]
;
$
group
[
$column
]
=
array
(
'$last'
=>
'$'
.
$column
)
;
}
}
// Return these results since we don't have a MongoCursor
// Apply aggregation functions
return
$results
;
if
(
$this
->
aggregate
)
{
foreach
(
$this
->
aggregate
[
'columns'
]
as
$column
)
{
$group
[
$column
]
=
array
(
'$'
.
$this
->
aggregate
[
'function'
]
=>
'$'
.
$column
);
}
}
if
(
$wheres
)
$pipeline
[]
=
array
(
'$match'
=>
$match
);
$pipeline
[]
=
array
(
'$group'
=>
$group
);
// Apply order and limit
if
(
$this
->
orders
)
$pipeline
[]
=
array
(
'$sort'
=>
$this
->
orders
);
if
(
$this
->
limit
)
$pipeline
[]
=
array
(
'$limit'
=>
$this
->
limit
);
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
// Return results
return
$results
[
'result'
];
}
}
else
else
{
{
// Get the MongoCursor
// Get the MongoCursor
$cursor
=
$this
->
collection
->
find
(
$this
->
compileWheres
(),
$this
->
columns
);
$cursor
=
$this
->
collection
->
find
(
$wheres
,
$this
->
columns
);
}
// Apply order
// Apply order, offset and limit
if
(
$this
->
orders
)
if
(
$this
->
orders
)
$cursor
->
sort
(
$this
->
orders
);
{
if
(
$this
->
offset
)
$cursor
->
skip
(
$this
->
offset
);
$cursor
->
sort
(
$this
->
orders
);
if
(
$this
->
limit
)
$cursor
->
limit
(
$this
->
limit
);
}
// Apply offset
// Return results
if
(
$this
->
offset
)
return
$cursor
;
{
$cursor
->
skip
(
$this
->
offset
);
}
}
}
// Apply limit
/**
if
(
$this
->
limit
)
* Execute an aggregate function on the database.
*
* @param string $function
* @param array $columns
* @return mixed
*/
public
function
aggregate
(
$function
,
$columns
=
array
(
'*'
))
{
$this
->
aggregate
=
compact
(
'function'
,
'columns'
);
$results
=
$this
->
get
(
$columns
);
if
(
isset
(
$results
[
0
]))
{
{
$cursor
->
limit
(
$this
->
limit
)
;
return
$results
[
0
][
$columns
[
0
]]
;
}
}
// Return results
return
$cursor
;
}
}
/**
/**
...
@@ -157,7 +170,7 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -157,7 +170,7 @@ class Query extends \Illuminate\Database\Query\Builder {
foreach
(
$groups
as
$group
)
foreach
(
$groups
as
$group
)
{
{
$this
->
groups
[
$group
]
=
1
;
$this
->
groups
[
$group
]
=
'$'
.
$group
;
}
}
return
$this
;
return
$this
;
...
@@ -246,18 +259,6 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -246,18 +259,6 @@ class Query extends \Illuminate\Database\Query\Builder {
return
$this
;
return
$this
;
}
}
/**
* Force the query to only return distinct results.
*
* @return Builder
*/
public
function
distinct
(
$column
=
false
)
{
$this
->
distinct
=
$column
;
return
$this
;
}
/**
/**
* Compile the where array
* Compile the where array
*
*
...
@@ -310,7 +311,7 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -310,7 +311,7 @@ class Query extends \Illuminate\Database\Query\Builder {
if
(
$boolean
==
'or'
)
if
(
$boolean
==
'or'
)
{
{
return
array
(
$this
->
conversion
[
$boolean
]
=>
array
(
$query
));
return
array
(
'$or'
=>
array
(
$query
));
}
}
return
$query
;
return
$query
;
...
@@ -325,7 +326,7 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -325,7 +326,7 @@ class Query extends \Illuminate\Database\Query\Builder {
if
(
$boolean
==
'or'
)
if
(
$boolean
==
'or'
)
{
{
return
array
(
$this
->
conversion
[
$boolean
]
=>
array
(
$compiled
));
return
array
(
'$or'
=>
array
(
$compiled
));
}
}
return
$compiled
;
return
$compiled
;
...
@@ -335,7 +336,7 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -335,7 +336,7 @@ class Query extends \Illuminate\Database\Query\Builder {
{
{
extract
(
$where
);
extract
(
$where
);
return
array
(
$column
=>
array
(
$this
->
conversion
[
'in'
]
=>
$values
));
return
array
(
$column
=>
array
(
'$in'
=>
$values
));
}
}
public
function
compileWhereNull
(
$where
)
public
function
compileWhereNull
(
$where
)
...
...
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