Quick Start
- PHP 8.0
- MongoDB Driver
Installation
You can install library through Composer:
composer require leroy-merlin-br/mongolid
Setup
If you are not using Laravel, you should initialize the Mongolid connection and container manually.
The minimalistic way of doing it is to use Mongolid\Connection\Manager
:
require 'vendor/autoload.php';
use Mongolid\Connection\Manager;
use Mongolid\Connection\Connection;
$manager = new Manager();
$manager->setConnection(new Connection('mongodb://localhost:27017'));
Now you are ready to create your own models 😄
Basic usage
class Post extends \Mongolid\Model\AbstractModel
{
}
Note that we did not tell Mongolid which collection to use for our Post
model. So, in this case, Mongolid will not save the model into the database.
This can be used for models that represents objects that will be embedded within another object and will not have their own collection.
You may specify a collection by defining a collection
property on your model:
class Post extends \Mongolid\Model\AbstractModel {
protected $collection = 'posts';
}
Mongolid will also assume each collection has a primary key attribute named _id
, since MongoDB requires an _id
for every single document.
The _id
attribute can be of any type. The default type for this attribute is ObjectId
.
Learn more about the MongoId.
Mongolid will automatically convert strings in ObjectId format (For example: "4af9f23d8ead0e1d32000000")
to ObjectId
when querying or saving an object.
Once a model is defined, you are ready to start retrieving and creating documents in your collection.
$posts = Post::all();
$post = Post::first('4af9f23d8ead0e1d32000000');
// or
$post = Post::first(new MongoDB\BSON\ObjectID('4af9f23d8ead0e1d32000000'));
$post = Post::first(['title' => 'How Mongolid saved the day']);
$posts = Post::where(['category' => 'coding']); // where() method returns a MongolidCursor
$posts = Post::where(['votes' => ['$gt' => 100]])->limit(10); // Mongolid\Cursor\Cursor
foreach ($posts as $post) {
var_dump($post->title);
}
$count = Post::where(['votes' => ['$gt' => 100]])->count(); // int
Pretty easy right?