MongoDB Extension for Yii 2

This extension provides the MongoDB integration for the Yii framework 2.0.

For license information check the LICENSE-file.

Documentation is at docs/guide/

Latest Stable Version Total Downloads Build Status


This extension requires MongoDB PHP Extension version 1.0.0 or higher.

This extension requires MongoDB server version 3.0 or higher.

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiisoft/yii2-mongodb

or add

"yiisoft/yii2-mongodb": "~3.0.0"

to the require section of your composer.json.


To use this extension, simply add the following code in your application configuration:

return [
    'components' => [
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://localhost:27017/mydatabase',
            'options' => [
                "username" => "Username",
                "password" => "Password"

Class Reference

Class Description
yii\mongodb\ActiveFixture ActiveFixture represents a fixture backed up by a MongoDB ActiveRecord class or a MongoDB collection.
yii\mongodb\ActiveQuery ActiveQuery represents a Mongo query associated with an Active Record class.
yii\mongodb\ActiveRecord ActiveRecord is the base class for classes representing Mongo documents in terms of objects.
yii\mongodb\BatchQueryResult BatchQueryResult represents a batch query from which you can retrieve data in batches.
yii\mongodb\Cache Cache implements a cache application component by storing cached data in a MongoDB.
yii\mongodb\ClientSession ClientSession represents a client session and Commands, queries, and write operations may then be associated the session.
yii\mongodb\Collection Collection represents the Mongo collection information.
yii\mongodb\Command Command represents MongoDB statement such as command or query.
yii\mongodb\Connection Connection represents a connection to a MongoDb server.
yii\mongodb\Database Database represents the MongoDB database information.
yii\mongodb\Exception Exception represents an exception that is caused by some Mongo-related operations.
yii\mongodb\LogBuilder LogBuilder allows composition and escaping of the log entries.
yii\mongodb\Migration Migration is the base class for representing a MongoDB migration.
yii\mongodb\Query Query represents Mongo "find" operation.
yii\mongodb\QueryBuilder QueryBuilder builds a MongoDB command statements.
yii\mongodb\Session Session extends yii\web\Session by using MongoDB as session data storage.
yii\mongodb\Transaction In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.
yii\mongodb\console\controllers\MigrateController Manages application MongoDB migrations.
yii\mongodb\debug\ExplainAction ExplainAction provides EXPLAIN information for MongoDB queries
yii\mongodb\debug\MongoDbPanel MongoDbPanel panel that collects and displays MongoDB queries performed.
yii\mongodb\file\ActiveQuery ActiveQuery represents a Mongo query associated with an file Active Record class.
yii\mongodb\file\ActiveRecord ActiveRecord is the base class for classes representing Mongo GridFS files in terms of objects.
yii\mongodb\file\Collection Collection represents the Mongo GridFS collection information.
yii\mongodb\file\Cursor Cursor is a wrapper around \MongoDB\Driver\Cursor, which allows returning of the record with yii\mongodb\file\Download instance attached.
yii\mongodb\file\Download Download represents the GridFS download operation.
yii\mongodb\file\Query Query represents Mongo "find" operation for GridFS collection.
yii\mongodb\file\StreamWrapper StreamWrapper provides stream wrapper for MongoDB GridFS, allowing file operations via regular PHP stream resources.
yii\mongodb\file\Upload Upload represents the GridFS upload operation.
yii\mongodb\gii\model\Generator This generator will generate ActiveRecord class for the specified MongoDB collection.
yii\mongodb\i18n\MongoDbMessageSource MongoDbMessageSource extends yii\i18n\MessageSource and represents a message source that stores translated messages in MongoDB collection.
yii\mongodb\log\MongoDbTarget MongoDbTarget stores log messages in a MongoDB collection.
yii\mongodb\rbac\MongoDbManager MongoDbManager represents an authorization manager that stores authorization information in MongoDB.
yii\mongodb\rbac\Permission Permission is a special version of yii\rbac\Permission dedicated to MongoDB RBAC implementation.
yii\mongodb\rbac\Role Role is a special version of yii\rbac\Role dedicated to MongoDB RBAC implementation.
yii\mongodb\validators\MongoDateValidator MongoDateValidator is an enhanced version of yii\validators\DateValidator, which supports \MongoDate values.
yii\mongodb\validators\MongoIdValidator MongoIdValidator verifies if the attribute is a valid Mongo ID.