A PHP program to delete documents from MongoDB collection

December 28, 2012
By

In this article, we will create a php program to delete documents from MongoDB collection.

The development environment for this application include Apache 2.2.22, PHP 5.3.10 and MongoDB 2.2.2.


1. Documents available in “students” collection before executing php program for deleting documents

> db.students.find()
{ "_id" : ObjectId("50d69d6a1d26a8a0c1eaecf8"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50d69de81d26a8a0c1eaecf9"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50d69e5bbe5a399f4650f9a3"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50d69e5bbe5a399f4650f9a4"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }
{ "_id" : ObjectId("50d69eb8be5a399f4650f9a5"), "name" : "Anjali", "age" : 20, "gender" : "F", "course" : "MBA", "marks" : 67 }
{ "_id" : ObjectId("50d69eb8be5a399f4650f9a6"), "name" : "Ali Khan", "age" : 20, "gender" : "M", "course" : "MBA", "marks" : 59 }
{ "_id" : ObjectId("50da9cd697f0d88a05000000"), "name" : "Mitra", "age" : 21, "course" : "BTECH", "gender" : "M", "marks" : 85 }
{ "_id" : ObjectId("50da9cd697f0d88a05000001"), "name" : "Banerjee", "age" : 20, "gender" : "M", "course" : "BTECH", "marks" : 70 }
{ "_id" : ObjectId("50da9cd697f0d88a05000002"), "name" : "Abdulla", "age" : 19, "gender" : "M", "course" : "BTECH", "marks" : 89 }

2. Create a file namely “students_delete.php” in the document root of the web server and add the given below php code

<?php

    /**
    User name : admu
    Password : new_pass
    MongoDB host : localhost
    MongoDB port : 27017
    Database : university
    */
    $server = "mongodb://admu:new_pass@localhost:27017/university";

    try{
        // Connecting to server
        $c = new MongoClient( $server );
    }catch(MongoConnectionException $connectionException){
        print $connectionException;
        exit;
    }

    try{
        $db = $c->university;
        $collection = $db->students;

        $qry = array("name"=>"Banerjee");
        $ret = $collection->remove($qry);

        var_dump($ret);

    }catch(MongoException $mongoException){
        print $mongoException;
        exit;
    }

Note : The above program deletes the student “Banerjee” from the “students” collection.


3. Execute the php program by opening its URL in a web browser

A PHP code to delete MongoDB data

Figure 1 : A PHP code to delete MongoDB data


4. Documents available in “students” collection after deleting document using php program

> db.students.find()
{ "_id" : ObjectId("50d69d6a1d26a8a0c1eaecf8"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50d69de81d26a8a0c1eaecf9"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50d69e5bbe5a399f4650f9a3"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50d69e5bbe5a399f4650f9a4"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }
{ "_id" : ObjectId("50d69eb8be5a399f4650f9a5"), "name" : "Anjali", "age" : 20, "gender" : "F", "course" : "MBA", "marks" : 67 }
{ "_id" : ObjectId("50d69eb8be5a399f4650f9a6"), "name" : "Ali Khan", "age" : 20, "gender" : "M", "course" : "MBA", "marks" : 59 }
{ "_id" : ObjectId("50da9cd697f0d88a05000000"), "name" : "Mitra", "age" : 21, "course" : "BTECH", "gender" : "M", "marks" : 85 }
{ "_id" : ObjectId("50da9cd697f0d88a05000002"), "name" : "Abdulla", "age" : 19, "gender" : "M", "course" : "BTECH", "marks" : 89 }

5. Download PHP Application


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , , ,

2 Responses to A PHP program to delete documents from MongoDB collection

  1. Eko Prasetya on November 9, 2013 at 3:15 pm

    Hi! i am very helpful with your post. Thanks a lot.
    I wanna ask you, if you may interest in making tutorial about connection between mongodb and android.
    I will appreciate it if you consider to make one.

  2. SOUMEN on October 30, 2015 at 11:49 am

    How do i delete using _id?

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner