Working with MongoDB using PHP (part 1)

Wednesday, 27th June 2018



Today we are going to see how to use MongoDB as a database for our PHP applications. This tutorial is divided into parts, each part covering one specific operation.

In this tutorial, we are going to build a simple blogging site using MongoDB as our database.

Note : In this tutorial we are going to use MongoDB from mLab. To know what it is and how to setup refer (Getting started with MongoDB DBaaS from mlab) article.

Layout of our simple blog:

  1. index.php : lists all the posts published on the blog
  2. post.php : displays entire content of the blog post
  3. newPost.php : use to add new post on the blog

First download the MongoDB library for PHP using composer. If you don't know what is composer and how to setup, refer Install composer on Ubuntu (Debian)

In your project directory create a file named composer.json and paste the following content in it unchanged.

	    "require": {
	        "mongodb/mongodb": "~1.2"

Now navigate to your project directory and run following command to download MongoDB library for PHP

	composer update

You'll get output something like below


composer update command will look for file composer.json and download all the components/libraries specified in it into a directory named vendor. Here we have specified mongodb so, it'll download mongodb library into the directory vendor at the root level of project directory. It'll also generate a autoload.php file in vendor directory.

This part completes the requirements needed to proceed further

Have anything to add or need help, just ping

Remember "Sharing is Caring"

Managed WordPress Hosting Static New


Thank You...

request failed! Contact admin.

Search Your Domain Name Here

WordPress Hosting Offer July