yii 2.0 using mongodb extension


I am newbie to yii 2.0. I am using MongoDb Extension for Yii 2 (http://www.yiiframework.com/doc-2.0/ext-mongodb-index.html).I wanted to use mongodb as database.How to create database in mongodb and how to access these db using yii 2.0. Also how and where to perform insertion, updation and deletion operation in yii 2.0.

thanx in advance…

You have to have a server with mongodb installed and use the command line. If you don’t have one you can use AMMPS locally, it can run yii2 with mongo or sql. If you use it, you can also use rockmongo (comes with ammps) to view your mongodb instead of using command line.

Once you have your server set up you can connect to the db like it shows in the yii2 docs for mongo.

You will need to create all of your crud like you do for sql but using mongo AR. Unlike mysql you don’t need to create your collections (tables in sql) because they will automatically be make from your AR model.

thankz skworden.

Is there is any way to check the connection between mongodb-yii is correct or not?

try using it, it will tell you if it’s not correct.

you literately don’t need anything except the connection to test if it’s working. You can do something like

public function actionIndex(){

$collection = Yii::$app->mongodb->getCollection('customer');

$collection->insert(['name' => 'John Smith', 'status' => 1]);

$collection->insert(['fname' => 'John','lname'=>'Smith', 'status' => 0,'addr'=>['street'=>'123 Main St.','city'=>'New York', 'state'=>'NY', 'zip'=>12345]]);

return $this->render('index');


they will create two records like


  "_id": ObjectId("5565373b6ec7ad1c1b000029"),

  "name": "John Smith",

  "status": 1,



  "_id": ObjectId("5565373b6ec7ad1c1b000029"),

  "fname": "John",

  "lname": "Smith",

  "status": 0,


	"street":"123 Main St.",

	"city":"New York",





remember whenever you need the _id you need to convert it to a string first

Url::to(['view','id'=>(string) $model->_id]);

in a controller action and it will create 2 new records with different attributes if the connection is right. You don’t have to create the collection it will do it for you with the attributes listed. If it goes then it works and note the above is just for demonstration purposes. Yii also recommends not nesting them like the second query but mongo does. I personally nest them but it can get tricky if you don’t know what you’re doing 100% for things like validation etc. Later versions of yii are going to have a better handle on the subdocs as well.

I used to only use sql type databases and it took about a two weeks to transition to nosql. They were painful two weeks but way worth is.

a few things to remember are:

  • Keep your attribute names down i.e. instead of first_name do something like fname.[list]
  • You don’t want to go to crazy shorting it where you don’t understand it but defiantly keep them down because unlike sql they are stored in every record so it will save tons of space shaving off a couple chars off of every attribute.

[] Don’t overload on sub docs. A good rule is that if it only will have no more than 5-10 per document or you will have some major performance issues (size will be the factor here).[]Take as much out of other collections as possible. i.e. if an order has a shipped to address keep in the orders collection instead of creating a new collection for shipped address.[]only return what you need in queries especially if you are using subdocs. if you only need name and phone tell the query to only return name and phone.[]every document doesn’t have to be the same! (one of the biggest perks)[]Take a look at gridfs for large files i.e. images, pdf’s etc. (and even small ones if you want)[]when querying make sure you are quering object ID by object ID not strings or it won’t find anything[*]You will never need a create at time attribute. The objectid has the created at timestamp in it so it’s just a waste of space.[/list]to get the timestamp i use something like this (extend yii’s mongoActiveRecord and this is a function i use to get the created timestamp if i need it).


* @return timestamp based on the mongodb objectId


public function getTimeStamp($format = NULL) {

 $mongoId = new \MongoId($this->getPrimaryKey());

 return date($format ? $format : 'Y-m-d H:i:s', $mongoId->getTimestamp());


<?= $model->timeStamp;?>

<?= $model->getTimeStamp('d M, Y');?>