An extended jenssegers/laravel-mongodb library
- This package is a wrapper around
jenssegers/mongodb
. Usages and configurations are the same as base package, except new features and changed behaviours. They have covered in the following sections. - This packages uses
jenssegers/mongodb
version3.0
and it is compatible with laravel5.1
and5.2
.
jenssegers/laravel-mongodb is a great package but it does not support DBRefs
(read more). The problem was that related model ID stored as
string (value of related document ObjectID), and not MongoDB ObjectID itself.
This is ok and work well until you want to join related collection by using lookup
aggregation (read more). In this package we change the way that the related document
ID stores.
In MongoDB, relations are different than SQL RDBMS. For example a One-to-Many
relation
can be categorized into three type; One-to-Few
, One-to-Many
, One-to-Squillions
. And
each of them store related IDs with different strategy. Other factors like whether the
related entity is weak or strong entity are effective in selecting the best strategy.
In this package we provided new model relations for covering these type of relations.
You can read more about this topic in these links.
- 6 Rules of Thumb for MongoDB Schema Design: Part 1
- 6 Rules of Thumb for MongoDB Schema Design: Part 2
- 6 Rules of Thumb for MongoDB Schema Design: Part 3
Add moloquent package to your compoer.json
:
"require": {
"kamva/moloquent": "~1.0"
}
And then do a composer update
.
Or run the following command on your console:
composer require kamva/moloquent ~1.0
Register the service provider within the providers array found in config/app.php:
'providers' => [
// ...
Kamva\Moloquent\MoloquentServiceProvider::class
]
Change your default database connection name in config/database.php:
'default' => env('DB_CONNECTION', 'mongodb'),
And add a new mongodb connection:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
You can connect to multiple servers or replica sets with the following configuration:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'local'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => []
],
Extend your models from Kamva/Moloquent/Moloquent
, and your done.
use Kamva/Moloquent/Moloquent;
class User extend Moloquent {}
Supported relations are:
- hasOne
- hasMany
- belongsTo
- belongsToMany
- embedsOne
- embedsMany
- ContainsOne
- ContainsFew
- IncludedIn
The first 6 relations are the same as base package, with a little different that IDs store as ObjectID instead of value of ObjectID (string)
It is One-to-One
's sister. The difference is that the child ID will store in parent
document. Here is an example:
use Kamva/Moloquent/Moloquent;
class User extend Moloquent {
public function avatar() {
return $this->containsOne('Image');
}
}
And the document on mongodb users collection would be like this:
{
"_id" : ObjectId("57bbca61551dfe007c67427e"),
"email" : "sample@mail.com",
"password" : "[hashed passowrd]",
"image_id" : ObjectId("57d2b82e1284c5008032521f")
}
It is a One-to-Many
relation that children are few, and children are strong entity
(or there's a need to access them directly). So we want to save the children IDs in parent.
Here is an example:
use Kamva/Moloquent/Moloquent;
class User extend Moloquent {
public function phones() {
return $this->containsFew('Phone');
}
}
And an example document would be like this:
{
"_id" : ObjectId("57bbca61551dfe007c67427e"),
"email" : "sample@mail.com",
"password" : "[hashed passowrd]",
"phone_ids" : [
ObjectId("57d2ba501284c500826dfbd5"),
ObjectId("57ba1e7d551dfe007a7e8cc1")
]
}
It is the opposite relation of ContainsOne
and ContainsFew
when the child (current instance) is included in
one parent.
use Kamva/Moloquent/Moloquent;
class Phone extend Moloquent {
public function owner() {
return $this->IncludedIn('User');
}
}
There are situations that the child is included in many instances of a model, for these situation we use
IncludedInMany
relation.
use Kamva/Moloquent/Moloquent;
class Address extend Moloquent {
public function order() {
return $this->IncludedInMany('Order');
}
}