Commit ade145c0 authored by Jens Segers's avatar Jens Segers

Update README.md

parent 3a561f0f
......@@ -12,175 +12,133 @@ Installation
Add the package to your `composer.json` or install manually.
```yaml
{
"require": {
"jenssegers/mongodb": "*"
{
"require": {
"jenssegers/mongodb": "*"
}
}
}
```
Run `composer update` to download and install the package.
Add the service provider in `app/config/app.php`:
```php
'Jenssegers\Mongodb\MongodbServiceProvider',
```
'Jenssegers\Mongodb\MongodbServiceProvider',
Usage
-----
Tell your model to use the MongoDB model and a MongoDB collection (alias for table):
```php
use Jenssegers\Mongodb\Model as Eloquent
class MyModel extends Eloquent {
protected $collection = 'mycollection';
}
```
use Jenssegers\Mongodb\Model as Eloquent
class MyModel extends Eloquent {
protected $collection = 'mycollection';
}
Configuration
-------------
The model will automatically check the database configuration array in `app/config/database.php` for a 'mongodb' item.
```php
'mongodb' => array(
'host' => 'localhost',
'port' => 27017,
'database' => 'database',
),
```
'mongodb' => array(
'host' => 'localhost',
'port' => 27017,
'database' => 'database',
),
You can also specify the connection name in the model:
```php
class MyModel extends Eloquent {
protected $connection = 'mongodb2';
}
```
class MyModel extends Eloquent {
protected $connection = 'mongodb2';
}
Examples
--------
**Retrieving All Models**
```php
$users = User::all();
```
$users = User::all();
**Retrieving A Record By Primary Key**
```php
$user = User::find('517c43667db388101e00000f');
```
$user = User::find('517c43667db388101e00000f');
**Wheres**
```php
$users = User::where('votes', '>', 100)->take(10)->get();
```
$users = User::where('votes', '>', 100)->take(10)->get();
**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**
```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.
**Using Where Between**
```php
$users = User::whereBetween('votes', array(1, 100))->get();
```
$users = User::whereBetween('votes', array(1, 100))->get();
**Where null**
```php
$users = User::whereNull('updated_at')->get();
```
$users = User::whereNull('updated_at')->get();
**Order By**
```php
$users = User::orderBy('name', 'desc')->get();
```
$users = User::orderBy('name', 'desc')->get();
**Offset & Limit**
```php
$users = User::skip(10)->take(5)->get();
```
$users = User::skip(10)->take(5)->get();
**Distinct**
Distinct requires a field for which to return the distinct values.
```php
$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();
```
$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();
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**
```php
$users = User::where('name', '=', 'John')->orWhere(function($query)
$users = User::where('name', '=', 'John')->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
```
**Group By**
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**
```php
$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');
```
$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');
Aggregations can be combined with **where**:
```php
$sold = Orders::where('sold', true)->sum('price');
```
$sold = Orders::where('sold', true)->sum('price');
**Like**
```php
$user = Comment::where('body', 'like', '%spam%')->get();
```
$user = Comment::where('body', 'like', '%spam%')->get();
**Inserts, updates and deletes**
......@@ -190,20 +148,14 @@ All basic insert, update, delete and select methods should be implemented.
Perform increments or decrements (default 1) on specified attributes:
```php
User::where('name', 'John Doe')->increment('age');
User::where('name', 'Bart De Wever')->decrement('weight', 50);
```
User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);
The number of updated objects is returned:
```php
$count = User->increment('age');
```
$count = User->increment('age');
You may also specify additional columns to update:
```php
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
```
\ No newline at end of file
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment