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


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
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.
How do i delete using _id?