Connect yii2 with mongodb Connect yii2 with mongodb mongodb mongodb

Connect yii2 with mongodb


If you are trying to install it through command prompt then try the following command which using composer

composer require --prefer-dist yiisoft/yii2-mongodb "*"

This works in my windows 8 environment.

To ignore dependency errors while installing the package use --ignore-platform-refs switch:

composer require --ignore-platform-refs --prefer-dist yiisoft/yii2-mongodb "*"


Remember that you must also have the MongoDB extension installed in PHP for this plugin to work:

http://php.net/manual/en/class.mongodb.php


Seems like problem was with yii2 composer dependencies, Kindly RUN this command in your console to add global dependencies,

1) composer global require "fxp/composer-asset-plugin:~1.1.1"

2) Add "yiisoft/yii2-mongodb": "~2.0.0" in your composer.json file

3) Now run composer install or composer update

4) Composer install will only install require packages in your composer file but composer update will also check if there is any new release for your packages that you mentioned in composer.js and then install the new release.

5) now add below code in your common/config/main.php file

return [    //....    'components' => [        'mongodb' => [            'class' => '\yii\mongodb\Connection',            'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',        ],    ],]; 

Now is the time to test either mongoDb working or not.

$collection = Yii::$app->mongodb->getCollection('customer');$collection->insert(['name' => 'John Smith', 'status' => 1]);

For further information Please follow below link,

https://github.com/yiisoft/yii2-mongodb