Quick start

Setting up MondocConfig, connecting to your MongoDB instance, and creating your first model and service.

Database connection

The first step is to establish your MongoDB connection details. This is done by creating a new instance of the MongoDB client and passing it to the MondocConfig class.

The MondocConfig object is a singleton that holds the MongoDB client instance and provides the mapping between your models and services.

MondocConfig supports multiple connections, and you can specify the connection identifier within your service classes. By default, the connection identifier is default. This feature allows you to hold user data in one database, and application data in another.

<?php

use MongoDB\Client;
use District5\Mondoc\MondocConfig;

$connection = new Client('mongodb+srv://user:pass@host:27017');
$database = $connection->selectDatabase('db_name');

$mondoc = MondocConfig::getInstance();
$mondoc->addDatabase(
    $database, // This is the MongoDB database object
    'default' // This is the connection identifier
);

// You can also add multiple connections...
// $otherConnection = new Client('mongodb+srv://user:pass@other-host:27017');
// $otherDatabase = $otherConnection->selectDatabase('other_db_name');
// $mondoc->addDatabase(
//     $otherDatabase, // This is the MongoDB database object
//     'user_data' // This is the connection identifier
// );

// We'll discuss this later...
// $mondoc->setServiceMap([
//     MyModel::class => MyService::class,
//     MyOtherModel::class => MyOtherService::class,
// ]);

// Or you can do it individually...
// $mondoc->addServiceMapping(
//     MyModel::class,
//     MyService::class
// );
// $mondoc->addServiceMapping(
//     MyOtherModel::class,
//     MyOtherService::class
// );

Defining data structure with MondocAbstractModel

Below is a basic example of a model class that represents a document.

If you want a deeper dive into the model class, please refer to the dedicated description Model page.
<?php

use District5\Mondoc\Db\Model\MondocAbstractModel;

class MyModel extends MondocAbstractModel
{
    protected string|null $name = null;

    public function getName(): string|null
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

Your first MondocAbstractService powered service

Below is a basic example of a service class that represents a MongoDB collection called my_collection.

If you want a deeper dive into the service class, please refer to the dedicated description Service page.
<?php

use District5\Mondoc\Db\MondocCollection;
use District5\Mondoc\Db\Service\MondocAbstractService;

use MongoDB\Database;
use MyModel;

/**
 * @method static MondocCollection getCollection()
 * @method static Database|null getMongo()
 * @method static MyModel[] getMultiByCriteria(array $filter = [], array $options = [])
 * @method static MyModel[] getMultiByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder)
 * @method static MyModel[] getByIds(array $ids)
 * @method static MyModel[] getMultiWhereKeyEqualsValue(string $key, $value)
 * @method static MyModel[] getMultiWhereKeyDoesNotEqualValue(string $key, $value)
 * @method static MyModel|null getOneByCriteria(array $filter = [], array $options = [])
 * @method static MyModel|null getById(\MongoDB\BSON\ObjectId|string $id)
 * @method static MyModel|null getOneByQueryBuilder(\District5\MondocBuilder\QueryBuilder $builder)
 * @method static MyModel|null getOneWhereKeyEqualsValue(string $key, mixed $value)
 * @method static MyModel|null getOneWhereKeyDoesNotEqualValue(string $key, mixed $value)
 * @method static MyModel[] getPage(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, ?string $sortByField = '_id', int $sortDirection = -1): array
 * @method static MyModel[] getPageByByObjectIdPagination(\District5\Mondoc\Helper\MondocPaginationHelper $paginator, \MongoDB\BSON\ObjectId|string|null $currentId, int $sortDirection = -1)
 */
class MyService extends MondocAbstractService
{
    protected static function getCollectionName(): string
    {
        return 'my_collection';
    }
}
At this point, Mondoc still does not know about your model and service. You need to let Mondoc know about them by linking them to MondocConfig.

Linking your model and service to MondocConfig

After creating your model and service, the final step is to add the service map to MondocConfig.

You can do this by either using the setServiceMap method, which overrides the whole service map, or by using the addServiceMapping method, which adds a single service to the map.

<?php

\District5\Mondoc\MondocConfig::getInstance()->setServiceMap([
    MyModel::class => MyService::class
]);

// Alternatively, you can do this:

\District5\Mondoc\MondocConfig::getInstance()->addServiceMapping(
    MyModel::class,
    MyService::class
);
Congratulations! You have successfully set up your first model and service with Mondoc.

Next steps...

Now that you have set up your first model and service, and connected to your database, you can start interacting with your MongoDB

You can read the more advanced topics by following the links below.

Modelfast_forward Servicefast_forward
Nested modelsfast_forward