What is MongoDb?
MongoDB is an open source document oriented nosql database. It is powerful, extremely fast, flexible and highly scalable database. It stores data in the form of JSON like documents BSON(Binary JSON).
Basics of Mongodb
Collection : In mongo database, data is stored in Collections. These are equivalent to tables in relational database.
Document : Document represent a record in MongoDB collection. We can store integer, string, array, objects and many more type of data as a document in collection.
Schemaless: Mongo has no schema for storing document, we can store any type of data in any cell of a row.
To install mongodb follow this article “How to install mongodb” and install andy GUI tool like MongoVue or Robomongo for better understanding.
Mongodb CRUD operations using PHP
Create records
Single Insert
Consider a scenario where we want to insert user details in “user_details” collection in “users” database.
1 2 3 4 5 6 7 8 9 10 11 |
// connecting to mongo (default localhost at port 27017) $mongo = new mongoClient(); // to connect mongo at a specfic IP with username and password, write it as follows $connection_string = "mongodb://mongo-username:mongo-password@ip"; $mongo = new mongoClient($connection_string); $data_to_insert = array('name'=>'Tushar Sharma','age'=>24,'skills'=>array('php','jquery')); try{ $mongo->users->user_details->insert($data_to_insert); } catch(exception $e) { // catch exception } |
That’s it, we have inserted a new record and i think you didn’t noticed one thing and that is “we don’t have to create any database or table(collection) or schema in order to create record in mongo“.
Now, if you are dealing with a single database then specify database as follows.
1 2 3 4 5 6 7 8 |
$mongo = new mongoClient(); $mongo_db = $mongo->selectDB("users"); $data_to_insert = array('name'=>'Tushar Sharma','age'=>24,'skills'=>array('php','jquery')); try{ $mongo_db->user_details->insert($data_to_insert); } catch(exception $e) { // catch exception } |
Last insert id can be obtained using following syntax, remember that mongo create a default unique and indexed id as _id and it’s an object not string. Check the image at the end.
1 |
$last_insert_id = $data_to_insert['_id']; |
Bulk Insert
Consider a scenario where we want to insert multiple records at once, then we write it as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$mongo = new mongoClient(); $batch_insert = array( array('name'=>'Shubham Sharma','age'=>'32', 'skills'=>array('java','struts'),'has_job'=>0), array('name'=>'Saurabh','skills'=>array('mysql','mongo'), 'has_job'=>1), array('name'=>'John Doe', 'skills'=>array('frontend'=>array('html','css','bootstrap'), 'backend'=>array('php','python','node js')),'has_job'=>1,'salary'=>'76k'), ); try{ //In case of error, continueOnError prevents stopping insertion while batch //inserting, it will left the document with error and continuing inserting //others documents with no error. $mongo->users->user_details->batchInsert($batch_insert,array('continueOnError' => true)); } catch(Exception $e){ echo $e->getMessage(); } |
Above batch insert shows that you can insert any data type in any field.
In first case, we entered age as string instead of integer (as inserted before).
In second case, we entered a new field has_job and also remove age.
In third case, we entered a multi dimensional array and added a new field salary.
Above example shows that mongo is schemaless.
Applying Indexes
Index is applied on a collection using following query
1 2 3 4 5 6 7 8 9 10 |
// apply compound index as // age descending and has_job ascending // on user_details collection $mongo->users->user_details->ensureIndex(array('age'=>-1,'has_job'=>1)); // apply unique index as age descending and it must be unique $mongo->users->user_details->ensureIndex( array('age'=>-1), array('unique' => true) ); |
Reading records
Consider a scenario where we want to read above records on the basis of various conditions, we will discuss each aspect one by one.
1 2 3 4 5 6 7 8 9 10 |
// get all user details from user_details collection $user_details_cursor = $mongo->users->user_details->find(); if( $user_details_cursor->hasNext() ) { while( $results = $user_details_cursor->getNext() ) { // prints each record individually print_r($results); } } |
find function is used to fetch data, it returns a cursor to the data. “hasNext()” ensures that whether any data found or not. After that we have to iterate over cursor to get results, for this “getNext()” function is used.
To count number of results which is obtained from find query writes as follows
1 2 |
// return number of results $total_results_count = $user_details_cursor->count() |
Find results with some specific conditions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// find users with age 24, // sort results by age in descending order // and limit results to 1 $mongo->users->user_details->find(array('age'=>24)) ->sort(array('age'=>-1)) ->limit(1); // find users with age greater than 20 and name tushar sharma, // sort results by age in ascending order // and limit results to 5 $mongo->users->user_details->find(array('age'=>array('$gt'=>20),'name'=>"tushar sharma")) ->sort(array('age'=>1)) ->limit(5); // find users with age greater than equal to 20 and name tushar sharma, // sort results by age in ascending order and has_job in descending order // and limit results to 5 $mongo->users->user_details->find(array('age'=>array('$gte'=>20),'name'=>"tushar sharma")) ->sort(array('age'=>1,'has_job'=>-1)) ->limit(5); // find users with age lest than 30, // sort results by age in ascending order and has_job in descending order // and limit results to 5 $mongo->users->user_details->find(array('age'=>array('$lt'=>30))) ->sort(array('age'=>1,'has_job'=>-1)) ->limit(5); // find users where no age is filled, // sort results by age in descending order // and limit results to 1 $mongo->users->user_details->find(array('age'=>array('$exists'=>false))) ->sort(array('age'=>-1)) ->limit(1); // find users where age is filled, // sort results by age in descending order // and return 5 results after skipping first 5 results $mongo->users->user_details->find(array('age'=>array('$exists'=>false))) ->sort(array('age'=>-1)) ->limit(5) ->skip(5); |
Updating records
Let’s update some of the user details which we have entered above.
1 2 3 4 |
// update ALL users whose age is 24 to age 25 and has-job as 1 $is_result_updated = $mongo->users->user_details->update(array('age'=>24), array('$set'=>array('age'=>25,'has_job'=>1)), array('multiple'=>true)); |
Here ‘multiple’ is used to updated all results with matching criteria, by default mongo will update only one result if we doesn’t pass third parameter in above query.
In the below image you can see data after updating records, in first case skills is an array and in second case it’s an object.
Below image shows when mongo is successfully connected then it shows connected as 1, second section shows last insert id and third section shows if update is successful then nModified and n is equal to number of rows affected.
Deleting records
Deleting records is the easiest of all, it is done using following query
1 2 |
// delete all users with age 24 $mongo->users->user_details->remove(array('age'=>24)) |
Always remember, delete by default works on all records and update works on single record.
That’s all in “Mongodb crud operations using php”.Do comment and subscribe for further updates.