MongoDB PHP Tutorial — 6 Steps to Connect MongoDB with PHP

Snehacynixit
3 min readJun 25, 2020

--

MongoDB PHP tutorial specially designs to connect MongoDB with PHP. Here, we will see the process with an example for clear understanding. So before wasting time, let’s discuss it. Learn more skills and techniques from MongoDB online course

MongoDB PHP

Download the MongoDB PHP driver, download the latest version of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory.
extension = php_mongo.dll

How to Connect MongoDB with PHP

Following are the few steps to connect MongoDB PHP:

1. Make a Connection and Select a Database

For connecting to the MongoDB database you need to specify the name of the database, if the database does not exist then MongoDB will create it automatically.

Following is the code for that:

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. ?>

When you execute the above program, it will display the following result:

Connection to the database successfully

Database examplesdb selected

2. Create a Collection

Code for creating a collection in MongoDB

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. $collection = $db->createCollection(“examplescol”);
  9. echo “Collection created succsessfully”;
  10. ?>

This is the output for the code:

Connection to the database successfully

3. Insert a Document

insert() method, is used to insert a document in MongoDB. best MongoDB course helps you to learn more effectively.

Following is the code for inserting a document:

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. $collection = $db->examplescol;
  9. echo “Collection selected succsessfully”;
  10. $document = array(
  11. “title” => “MongoDB”,
  12. “description” => “database”,
  13. “likes” => 100,
  14. “url” => “http://www.data-flair.training/mongodb/",
  15. “by” => “data flair”
  16. );
  17. $collection->insert($document);
  18. echo “Document inserted successfully”;
  19. ?>

After executing the code you will get the following output:

Connection to the database successfully

Database examplesdb selected

Collection selected successfully

Document inserted successfully

4. Find All Documents

find() method is used to select all documents from the collection.

Following is the code to find all documents:

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. $collection = $db->examplescol;
  9. echo “Collection selected succsessfully”;
  10. $cursor = $collection->find();
  11. // iterate cursor to display title of documents
  12. foreach ($cursor as $document) {
  13. echo $document[“name”] . “\n”;
  14. }
  15. ?>

After executing the following code you will get this output:

  1. Connection to database successfully
  2. Database examplesdb selected
  3. Collection selected succsessfully {
  4. “name”: “MongoDB”
  5. }

5. Update a Document

update() method is used to update a document in MongoDB.

Following is the code to update a document:

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. $collection = $db->examplescol;
  9. echo “Collection selected succsessfully”;
  10. // now update the document
  11. $collection->update(array(“name”=>”MongoDB”),
  12. array(‘$set’=>array(“name”=>”MongoDB Tutorial”)));
  13. echo “Document updated successfully”;
  14. // now display the updated document
  15. $cursor = $collection->find();
  16. // iterate cursor to display title of documents
  17. echo “Updated document”;
  18. foreach ($cursor as $document) {
  19. echo $document[“name”] . “\n”;
  20. }
  21. ?>

After executing the program you will get the following output:

  1. Connection to the database successfully
  2. Database examplesdb selected
  3. Collection selected successfully
  4. Document updated successfully
  5. Updated document {
  6. “name”: “MongoDB Tutorial”
  7. }

6. Delete a Document

remove() method is used to delete a document in MongoDB.

Following is the code to delete a document:

  1. <?php
  2. // connect to mongodb
  3. $m = new MongoClient();
  4. echo “Connection to database successfully”;
  5. // select a database
  6. $db = $m->examplesdb;
  7. echo “Database examplesdb selected”;
  8. $collection = $db->examplescol;
  9. echo “Collection selected succsessfully”;
  10. // now remove the document
  11. $collection->remove(array(“name”=>”MongoDB Tutorial”),false);
  12. echo “Documents deleted successfully”;
  13. // now display the available documents
  14. $cursor = $collection->find();
  15. // iterate cursor to display title of documents
  16. echo “Updated document”;
  17. foreach ($cursor as $document) {
  18. echo $document[“name”] . “\n”;
  19. }
  20. ?>

After executing the program you will get the following output:

Connection to the database successfully

Database examplesdb selected

Collection selected successfully

Documents deleted successfully

Summary

So, this was all about the MongoDB PHP tutorial, in which we learn 6 steps to connect MongoDB with PHP with examples. Hope, you liked the explanation.

If you want to Gain In-depth Knowledge on MongoDB, please go through this link MongoDB Online Training

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response