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
ade145c0
Commit
ade145c0
authored
Jul 21, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update README.md
parent
3a561f0f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
98 deletions
+50
-98
README.md
README.md
+50
-98
No files found.
README.md
View file @
ade145c0
...
@@ -12,175 +12,133 @@ Installation
...
@@ -12,175 +12,133 @@ Installation
Add the package to your
`composer.json`
or install manually.
Add the package to your
`composer.json`
or install manually.
```
yaml
{
{
"require":
{
"
require"
:
{
"jenssegers/mongodb": "*"
"
jenssegers/mongodb"
:
"
*"
}
}
}
}
```
Run
`composer update`
to download and install the package.
Run
`composer update`
to download and install the package.
Add the service provider in
`app/config/app.php`
:
Add the service provider in
`app/config/app.php`
:
```
php
'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\MongodbServiceProvider'
,
```
Usage
Usage
-----
-----
Tell your model to use the MongoDB model and a MongoDB collection (alias for table):
Tell your model to use the MongoDB model and a MongoDB collection (alias for table):
```
php
use Jenssegers
\M
ongodb
\M
odel as Eloquent
use
Jenssegers\Mongodb\Model
as
Eloquent
class MyModel extends Eloquent {
class
MyModel
extends
Eloquent
{
protected $collection = 'mycollection';
protected
$collection
=
'mycollection'
;
}
}
```
Configuration
Configuration
-------------
-------------
The model will automatically check the database configuration array in
`app/config/database.php`
for a 'mongodb' item.
The model will automatically check the database configuration array in
`app/config/database.php`
for a 'mongodb' item.
```
php
'mongodb' => array(
'mongodb'
=>
array
(
'host' => 'localhost',
'host'
=>
'localhost'
,
'port' => 27017,
'port'
=>
27017
,
'database' => 'database',
'database'
=>
'database'
,
),
),
```
You can also specify the connection name in the model:
You can also specify the connection name in the model:
```
php
class MyModel extends Eloquent {
class
MyModel
extends
Eloquent
{
protected $connection = 'mongodb2';
protected
$connection
=
'mongodb2'
;
}
}
```
Examples
Examples
--------
--------
**Retrieving All Models**
**Retrieving All Models**
```
php
$users = User::all();
$users
=
User
::
all
();
```
**Retrieving A Record By Primary Key**
**Retrieving A Record By Primary Key**
```
php
$user = User::find('517c43667db388101e00000f');
$user
=
User
::
find
(
'517c43667db388101e00000f'
);
```
**Wheres**
**Wheres**
```
php
$users = User::where('votes', '>', 100)->take(10)->get();
$users
=
User
::
where
(
'votes'
,
'>'
,
100
)
->
take
(
10
)
->
get
();
```
**Or Statements**
**Or Statements**
```
php
$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
$users
=
User
::
where
(
'votes'
,
'>'
,
100
)
->
orWhere
(
'name'
,
'John'
)
->
get
();
```
**Using Where In With An Array**
**Using Where In With An Array**
```
php
$users = User::whereIn('age', array(16, 18, 20))->get();
$users
=
User
::
whereIn
(
'age'
,
array
(
16
,
18
,
20
))
->
get
();
```
When using
`whereNotIn`
objects will be returned if the field is non existant. Combine with
`whereNotNull('age')`
to leave out those documents.
When using
`whereNotIn`
objects will be returned if the field is non existant. Combine with
`whereNotNull('age')`
to leave out those documents.
**Using Where Between**
**Using Where Between**
```
php
$users = User::whereBetween('votes', array(1, 100))->get();
$users
=
User
::
whereBetween
(
'votes'
,
array
(
1
,
100
))
->
get
();
```
**Where null**
**Where null**
```
php
$users = User::whereNull('updated_at')->get();
$users
=
User
::
whereNull
(
'updated_at'
)
->
get
();
```
**Order By**
**Order By**
```
php
$users = User::orderBy('name', 'desc')->get();
$users
=
User
::
orderBy
(
'name'
,
'desc'
)
->
get
();
```
**Offset & Limit**
**Offset & Limit**
```
php
$users = User::skip(10)->take(5)->get();
$users
=
User
::
skip
(
10
)
->
take
(
5
)
->
get
();
```
**Distinct**
**Distinct**
Distinct requires a field for which to return the distinct values.
Distinct requires a field for which to return the distinct values.
```
php
$users = User::distinct()->get(array('name'));
$users
=
User
::
distinct
()
->
get
(
array
(
'name'
));
// or
// or
$users = User::distinct('name')->get();
$users
=
User
::
distinct
(
'name'
)
->
get
();
```
Distinct can be combined with
**where**
:
Distinct can be combined with
**where**
:
```
php
$users = User::where('active', true)->distinct('name')->get();
$users
=
User
::
where
(
'active'
,
true
)
->
distinct
(
'name'
)
->
get
();
```
**Advanced Wheres**
**Advanced Wheres**
```
php
$users = User::where('name', '=', 'John')->orWhere(function($query)
$users
=
User
::
where
(
'name'
,
'='
,
'John'
)
->
orWhere
(
function
(
$query
)
{
{
$query->where('votes', '>', 100)
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
->where('title', '<>', 'Admin');
})
})
->get();
->get();
```
**Group By**
**Group By**
Selected columns that are not grouped will be aggregated with the $last function.
Selected columns that are not grouped will be aggregated with the $last function.
```
php
$users = Users::groupBy('title')->get(array('title', 'name'));
$users
=
Users
::
groupBy
(
'title'
)
->
get
(
array
(
'title'
,
'name'
));
```
**Aggregation**
**Aggregation**
```
php
$total = Order::count();
$total
=
Order
::
count
();
$price = Order::max('price');
$price
=
Order
::
max
(
'price'
);
$price = Order::min('price');
$price
=
Order
::
min
(
'price'
);
$price = Order::avg('price');
$price
=
Order
::
avg
(
'price'
);
$total = Order::sum('price');
$total
=
Order
::
sum
(
'price'
);
```
Aggregations can be combined with
**where**
:
Aggregations can be combined with
**where**
:
```
php
$sold = Orders::where('sold', true)->sum('price');
$sold
=
Orders
::
where
(
'sold'
,
true
)
->
sum
(
'price'
);
```
**Like**
**Like**
```
php
$user = Comment::where('body', 'like', '%spam%')->get();
$user
=
Comment
::
where
(
'body'
,
'like'
,
'%spam%'
)
->
get
();
```
**Inserts, updates and deletes**
**Inserts, updates and deletes**
...
@@ -190,20 +148,14 @@ All basic insert, update, delete and select methods should be implemented.
...
@@ -190,20 +148,14 @@ All basic insert, update, delete and select methods should be implemented.
Perform increments or decrements (default 1) on specified attributes:
Perform increments or decrements (default 1) on specified attributes:
```
php
User::where('name', 'John Doe')->increment('age');
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
);
User::where('name', 'Jaques')->decrement('weight', 50);
User
::
where
(
'name'
,
'Bart De Wever'
)
->
decrement
(
'weight'
,
50
);
```
The number of updated objects is returned:
The number of updated objects is returned:
```
php
$count = User->increment('age');
$count
=
User
->
increment
(
'age'
);
```
You may also specify additional columns to update:
You may also specify additional columns to update:
```
php
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User
::
where
(
'age'
,
'29'
)
->
increment
(
'age'
,
1
,
array
(
'group'
=>
'thirty something'
));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
User
::
where
(
'bmi'
,
30
)
->
decrement
(
'bmi'
,
1
,
array
(
'category'
=>
'overweight'
));
```
\ 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