Mongodb CRUD operations using PHP

mongodb-crud-operations-using-php 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.

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“.

mongo-insert-using-php

Now, if you are dealing with a single database then specify database as follows.

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.

Bulk Insert
Consider a scenario where we want to insert multiple records at once, then we write it as follows.

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

 

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.

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

Find results with some specific conditions

Updating records

Let’s update some of the user details which we have entered above.

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.

mongo-array-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.

mongo-connection-update

Deleting records

Deleting records is the easiest of all, it is done using following query

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.

You may also like...

Leave a Reply