A PHP program to update collection data in MongoDB database with upsert option

December 28, 2012
By

In our previous article titled “A PHP program to insert individual and batch data into MongoDB database“, we have developed a php application to insert data into MongoDB collection.

In this article, we will create a php program which updates existing data with new data according to the criteria set. If no data is found that matches the criteria, then new data will be inserted into the collection provided the upsert option is set true.

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

> use university
switched to db university

> db.auth("admu","new_pass")
1

> 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" : 77 }
{ "_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_update.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;

        $criteria = array("name"=>"mitra");
        $newdata = array('$set'=>array("marks"=>85));
        $options = array("upsert"=>true,"multiple"=>true);

        $ret = $collection->update(
            $criteria,
            $newdata,
            $options
        );
        var_dump($ret);

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

Note : In this program, we are updating the marks of student “Mitra” from 77 to 85


3. Open the URL in a web browser

Updating MongoDB collection from a PHP program

Figure 1 : Updating MongoDB collection from a PHP program


4. Documents available in “students” collection after executing 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("50da9cd697f0d88a05000001"), "name" : "Banerjee", "age" : 20, "gender" : "M", "course" : "BTECH", "marks" : 70 }
{ "_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: , , , ,

One Response to A PHP program to update collection data in MongoDB database with upsert option

  1. prasad on October 10, 2015 at 7:55 pm

    how to update info in php using monodb and select query

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