MongoDB PHP Perform Create Retrieve Update Delete

MongoDB is a document type NoSQL database and currently is the most popular NoSQL database use by many developers over the world. All of us curious that why MongoDB is better than MySQL in some area. MongoDB is non-relational database and based on BASE principle which are Basically, Available, Soft State and Eventually Consistent. It is good for handle large volumes of data compare to MySQL and its support unstructured and unpredictable data which dint need a lot of time to construct a database. The performance of MongoDB is quite fast compare to RBMS database like MySQL and MS SQL. In this tutorial, I will tell you how to connect mongodb php perform create retrieve update delete features in your application. This is just very simple.

First, you need to have setup mongodb server to operate in your local computer.

Connect to MongoDB server

Create a php file name “db_connect” and this file will be connect to mongodb server and select the database from your mongodb server. If the database dont exist in your server, it will be automatically create for you.

Create a Collection

After that, create another file “create_collection” and copy paste the source code below in this file. It will be create the collection for your database.

Insert a Document

Now, add a new php file “insert” and this file will be add the new document in the “collection”. The array filed are title, description and price.

Retrieve all Documents

Create “find” php file and it will be retrieve all documents from the selected collection.

Update a Document

Create a file name “update” and implement the update document feature by the sample source code below.

Delete a Document

Lastly for delete a document, create a php file “delete” and copy the source code below the perform delete.

Run your Code

In Conclusion, you can now run your php source code in your web server, you will see the changes in your mongodb database. If you cannot view mongodb database from your server, you can download mongodb gui client (MongoBooster).

(MongoDB PHP Perform Create Retrieve Update Delete)

Source Code

(Visited 268 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. sani says:

    IS there a way to recover deleted collection?

Leave a Reply

Your email address will not be published. Required fields are marked *