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
5bd7ee64
Commit
5bd7ee64
authored
Aug 09, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding query projections, fixes #267
parent
79b6230a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
5 deletions
+54
-5
README.md
README.md
+6
-0
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+32
-5
QueryBuilderTest.php
tests/QueryBuilderTest.php
+16
-0
No files found.
README.md
View file @
5bd7ee64
...
@@ -564,6 +564,12 @@ Update or insert a document. Additional options for the update method are passed
...
@@ -564,6 +564,12 @@ Update or insert a document. Additional options for the update method are passed
DB::collection('users')->where('name', 'John')
DB::collection('users')->where('name', 'John')
->update($data, array('upsert' => true));
->update($data, array('upsert' => true));
**Projections**
You can apply projections to your queries using the
`project()`
method.
DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();
**Push**
**Push**
Add an items to an array.
Add an items to an array.
...
...
src/Jenssegers/Mongodb/Query/Builder.php
View file @
5bd7ee64
...
@@ -18,6 +18,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -18,6 +18,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
protected
$collection
;
protected
$collection
;
/**
* The column projections.
*
* @var array
*/
public
$projections
;
/**
/**
* All of the available clause operators.
* All of the available clause operators.
*
*
...
@@ -59,6 +66,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -59,6 +66,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
$this
->
connection
=
$connection
;
$this
->
connection
=
$connection
;
}
}
/**
* Set the projections.
*
* @param array $columns
* @return $this
*/
public
function
project
(
$columns
)
{
$this
->
projections
=
is_array
(
$columns
)
?
$columns
:
func_get_args
();
return
$this
;
}
/**
/**
* Execute a query for a single record by ID.
* Execute a query for a single record by ID.
*
*
...
@@ -152,9 +172,10 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -152,9 +172,10 @@ class Builder extends \Illuminate\Database\Query\Builder {
$pipeline
[]
=
array
(
'$group'
=>
$group
);
$pipeline
[]
=
array
(
'$group'
=>
$group
);
// Apply order and limit
// Apply order and limit
if
(
$this
->
orders
)
$pipeline
[]
=
array
(
'$sort'
=>
$this
->
orders
);
if
(
$this
->
orders
)
$pipeline
[]
=
array
(
'$sort'
=>
$this
->
orders
);
if
(
$this
->
offset
)
$pipeline
[]
=
array
(
'$skip'
=>
$this
->
offset
);
if
(
$this
->
offset
)
$pipeline
[]
=
array
(
'$skip'
=>
$this
->
offset
);
if
(
$this
->
limit
)
$pipeline
[]
=
array
(
'$limit'
=>
$this
->
limit
);
if
(
$this
->
limit
)
$pipeline
[]
=
array
(
'$limit'
=>
$this
->
limit
);
if
(
$this
->
projections
)
$pipeline
[]
=
array
(
'$project'
=>
$this
->
projections
);
// Execute aggregation
// Execute aggregation
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
$results
=
$this
->
collection
->
aggregate
(
$pipeline
);
...
@@ -179,11 +200,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -179,11 +200,19 @@ class Builder extends \Illuminate\Database\Query\Builder {
else
else
{
{
$columns
=
array
();
$columns
=
array
();
// Convert select columns to simple projections.
foreach
(
$this
->
columns
as
$column
)
foreach
(
$this
->
columns
as
$column
)
{
{
$columns
[
$column
]
=
true
;
$columns
[
$column
]
=
true
;
}
}
// Add custom projections.
if
(
$this
->
projections
)
{
$columns
=
array_merge
(
$columns
,
$this
->
projections
);
}
// Execute query and get MongoCursor
// Execute query and get MongoCursor
$cursor
=
$this
->
collection
->
find
(
$wheres
,
$columns
);
$cursor
=
$this
->
collection
->
find
(
$wheres
,
$columns
);
...
@@ -543,8 +572,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -543,8 +572,6 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
*/
public
function
pull
(
$column
,
$value
=
null
)
public
function
pull
(
$column
,
$value
=
null
)
{
{
var_dump
(
$value
);
// Check if we passed an associative array.
// Check if we passed an associative array.
$multipleValues
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
$multipleValues
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
...
...
tests/QueryBuilderTest.php
View file @
5bd7ee64
...
@@ -611,4 +611,20 @@ class QueryBuilderTest extends TestCase {
...
@@ -611,4 +611,20 @@ class QueryBuilderTest extends TestCase {
$this
->
assertEquals
(
1
,
$user
[
'age'
]);
$this
->
assertEquals
(
1
,
$user
[
'age'
]);
}
}
public
function
testProjections
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'name'
=>
'fork'
,
'tags'
=>
array
(
'sharp'
,
'pointy'
)),
array
(
'name'
=>
'spork'
,
'tags'
=>
array
(
'sharp'
,
'pointy'
,
'round'
,
'bowl'
)),
array
(
'name'
=>
'spoon'
,
'tags'
=>
array
(
'round'
,
'bowl'
)),
));
$results
=
DB
::
collection
(
'items'
)
->
project
(
array
(
'tags'
=>
array
(
'$slice'
=>
1
)))
->
get
();
foreach
(
$results
as
$result
)
{
$this
->
assertEquals
(
1
,
count
(
$result
[
'tags'
]));
}
}
}
}
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