In my last post, we checked basics about NoSQL database.If you have not read about NoSQL yet, read it https://www.thecreativedev.com/how-to-use-nosql-and-mongodb/here. In this article, I will show you how you can use MongoDB database.
What is MongoDB
MongoDB is document oriented database means everything in MongoDB is in a document and documents represented as JSON.
MongoDB is mainly implemented for several reasons:
- Its Uses JSON rather than XML
- Its Scalable
- High Performance
- Support Indexing
- Fast and easy to use
MongoDB is schema-less and there is not a notion of rigid table Structure.Data stored in MongoDB as the document so there is no need of Join Operation. At first it might seem odd but as you will see, its takes data quite nicely and easily.
How to install MongoDB in Windows:
Here are the steps to install MongoDB in Windows
INSTALLATION:
Step 1:
First of all download dll file for MongoDB (as per version of PHP you are using),php_mongo.dll from here. After than add php_mongo.dll file in your php extension directory(Mostly its path like C:PHPext). Next is to add below line in your php.ini file.
1 2 3 | extension=php_mongo.dll |
Step 2:
After following first step, Create one folder or directory in c drive like below:
1 2 3 4 | c:> mkdirdata c:> mkdirdatadb |
Step 3:
Now download MongoDB from Here. Unzip the downloaded folder and rename it to “Mongo”.
Step 4:
To connect with database, first of all, run the mongod.exe from command prompt
1 2 3 4 | C:> cd Mongobin C:Mongobin> mongod |
and your MongoDB server will start running.After that start to run the mongo.exe file.
1 2 3 4 | C:> cd Mongobin C:Mongobin> mongo |
And That’s all. you are connected with MongoDB.
How to connect MongoDB Database
As I have explained above that MongoDB stores data as documents which is like a simple associative array. So All the queries and operations in MongoDB are performed with the array.
The database connection code is very similar to code for connecting with MySQL or other types of databases:
To Connect with database server at localhost port 27017:
1 2 3 | $connection = new Mongo(); |
To Connect with a remote host with optional custom port:
1 2 3 4 | $connection = new Mongo( "192.168.2.1" ); $connection = new Mongo( "192.168.2.1:65432" ); |
When the connection to MongoDB server is established, Now it is a time to select a database to work with. If no database exists, a new database is created.
Currently there are two ways to do this:
1 2 3 4 | $db = $connection->selectDB('dbname'); $db = $connection->dbname; |
Note : In MongoDB, tables are known as collections.
Then we need to select a collection of selected database like we would pick a table of selected relational databases.
1 2 3 | $collection = $db->selectCollection('person'); |
or simply
1 2 3 | $collection = $db->person; |
How to insert New documents in MongoDB
The collection object should be used to perform basic operations to manipulate its information. For instance, if want to store information about a person you use code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $person = array( 'name' => 'Bhumi Shah', 'email' => 'bhumi@test.com', 'address' => array( array( 'country' => 'MI', 'zip' => '4821', 'address1' => '5 Corner Street' ), array( 'country' => 'CA', 'zip' => '4830', 'address1' => '5925, Wall street' ), ), 'sessions' => 0, ); $safe_insert = true; $collection->insert($person, $safe_insert); // To add record in document. $person_identifier = $person['_id']; |
How to update existing documents in MongoDB
1 2 3 4 5 6 7 8 9 10 11 12 | $record = array('email => 'bhumi@test.com'); $new_data = array( '$inc' => array('sessions' => 1), 'address.0.address2' => 'Some other address', ), '$unset' => array('address.1' => 1), ); $options['multiple'] = false;// To update single record $collection->update($record, $new_data, $options); //To update record |
Above statement update document by adding new field address2 for user “bhumi@test.com” and unset address.1 will unset the address1 field from the document. multiple = false is for to update the single record with email id “bhumi@test.com, if true update multiple records.
How to Delete documents in MongoDB
Deleting a document is very similar to retrieving or update documents.
1 2 3 4 | $record = array('field' => 'address'); $collection->remove($record); // To delete record from the document |
The main purpose to use MongoDB is speed and its simplicity because most databases are too much slow to fetch data and PHP needs to wait for databases most of the time and web development is impossible without database so if you want to work fast with the database, MongoDB is best option you have.
The collection object should be used to perform basic operations to manipulate its information. For instance, if want to store information about a person you use code like this:
Let’s see one example of Contact form
Contact form contain contact information of user like user contact number if user has multiple contact number like residential, mobile number, company number and even if you want to store like this more information then you need to take relational table for same and if we move to relational table, we need to apply Join query to retrieve information of user in our traditional database whereas in MongoDB everything is stored in document so there is no need of JOIN operation.
Everything stored in MongoDB is in one document, all addresses are associated in the same record without JOINs and if I would like to include a collection of contact information of a user, I can simply create a embedded docs in MongoDB.
I hope you have enjoyed this short post. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates. Do you have any question in MongoDB? Let me know via comment section.