Category: php Mongodb

  • Khóa học miễn phí PHP & MongoDB – Insert Document nhận dự án làm có lương

    PHP & MongoDB – Insert Document



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a bulkWrite object to insert record(s) in the collection.

    // Create a BulkWrite Object
    $bulk = new MongoDBDriverBulkWrite([''ordered'' => true]);
    
    $bulk->insert([''First_Name'' => "Mahesh",
    	''Last_Name'' => ''Parashar'',
    	''Date_Of_Birth'' => ''1990-08-21'',
    	''e_mail'' => ''mahesh_parashar.123@gmail.com'',
    	''phone'' => ''9034343345'']);
    
    // Execute the commands.
    $result = $manager->executeBulkWrite(''myDb.sampleCollection'', $bulk);
    

    Example

    Try the following example to insert documents in a collection in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          $bulk = new MongoDBDriverBulkWrite([''ordered'' => true]);
    
          $bulk->insert([''First_Name'' => "Mahesh",
             ''Last_Name'' => ''Parashar'',
             ''Date_Of_Birth'' => ''1990-08-21'',
             ''e_mail'' => ''mahesh_parashar.123@gmail.com'',
             ''phone'' => ''9034343345'']);
    
          $bulk->insert([''First_Name'' => "Radhika",
             ''Last_Name'' => ''Sharma'',
             ''Date_Of_Birth'' => ''1995-09-26'',
             ''e_mail'' => ''radhika_sharma.123@gmail.com'',
             ''phone'' => ''9000012345'']);
    
          $bulk->insert([''First_Name'' => "Rachel",
             ''Last_Name'' => ''Christopher'',
             ''Date_Of_Birth'' => ''1990-02-16'',
             ''e_mail'' => ''rachel_christopher.123@gmail.com'',
             ''phone'' => ''9000054321'']);
    
          $bulk->insert([''First_Name'' => "Fathima",
             ''Last_Name'' => ''Sheik'',
             ''Date_Of_Birth'' => ''1990-02-16'',
             ''e_mail'' => ''fathima_sheik.123@gmail.com'',
             ''phone'' => ''9000012345'']);
    
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          $result = $manager->executeBulkWrite(''myDb.sampleCollection'', $bulk);
    
          printf("Inserted %d document(s).n", $result->getInsertedCount());
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    Inserted 4 document(s).
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Environment Setup nhận dự án làm có lương

    PHP & MongoDB – Environment Setup



    Install MongoDB database

    Follow the MongoDB installation steps using

    Install PHP

    Follow the PHP installation steps using

    PHP MongoDB Driver

    To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url . Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory (“ext” by default) and add the following line to your php.ini file −

    extension = php_mongo.dll
    

    In case of Windows, make sure libsasl.dll is in the windows”s PATH. This dll is available in PHP installation directory.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Home nhận dự án làm có lương

    PHP & MongoDB Tutorial

    PHP & MongoDB Tutorial







    PHP based application can connect to MongoDB using PHP MongoDB Driver. PHP MongoDB Driver works with PHP on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

    Audience

    This tutorial is designed for PHP programmers who would like to understand the MongoDB driver to connect to MongoDB in detail along with its architecture and actual usage.

    Prerequisites

    Before proceeding with this tutorial, you should have a good understanding of PHP programming language. As you are going to deal with MongoDB database, you should have prior exposure to NoSQL and Database concepts.

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Overview nhận dự án làm có lương

    PHP & MongoDB – Overview



    PHP developer team has provided MongoDB Driver for PHP and have various resources available for it.

    First step in connecting to MongoDB using PHP is to have mongodb PHP driver dll in php ext directory and enable it in php.ini and then use mongodb API to connect to the database.

    Connecting to MongoDB database

    Suppose, MongoDB is installed locally and using default port then following syntax connects to MongoDB database.

    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    MongoDB Driver Manager is the central class to do all the operations on mongodb database. To connect to mongodb database, we connect to database by first preparing a command and then executing it.

    $statistics = new MongoDBDriverCommand(["dbstats" => 1]);
    $cursor = $manager->executeCommand("mydb", $statistics);
    

    Once command is executed, if mydb database is not present, it will be created otherwise, it will be connected.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Drop Collection nhận dự án làm có lương

    PHP & MongoDB – Drop Collection



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command to drop the collection.

    // Create a Command Instance
    $createCollection = new MongoDBDriverCommand(["drop" => "sampleCollection"]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("myDb", $createCollection);
    

    Example

    Try the following example to drop a collection in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          // Create a Command Instance
          $dropCollection = new MongoDBDriverCommand(["drop" => "sampleCollection"]);
    
          // Execute the command on the database
          $cursor = $manager->executeCommand("myDb", $dropCollection);
    
          echo "Collection dropped."
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    Collection dropped.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Create Collection nhận dự án làm có lương

    PHP & MongoDB – Create Collection



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command to create the collection.

    // Create a Command Instance
    $createCollection = new MongoDBDriverCommand(["create" => "sampleCollection"]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("myDb", $createCollection);
    

    Example

    Try the following example to create a collection in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          // Create a Command Instance
          $createCollection = new MongoDBDriverCommand(["create" => "sampleCollection"]);
    
          // Execute the command on the database
          $cursor = $manager->executeCommand("myDb", $createCollection);
    
          echo "Collection created.";
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    Collection created.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Drop Database nhận dự án làm có lương

    PHP & MongoDB – Drop Database



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command to drop the database.

    // Create a Command Instance
    $dropDatabase = new MongoDBDriverCommand(["dropDatabase" => 1]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("myDb", $dropDatabase);
    

    Example

    Try the following example to delete a database in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          // Create a Command Instance
          $dropDatabase = new MongoDBDriverCommand(["dropDatabase" => 1]);
    
          // Execute the command on the database
          $cursor = $manager->executeCommand("myDb", $dropDatabase);
    
          echo "Database dropped."
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    Database dropped.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Connect Database nhận dự án làm có lương

    PHP & MongoDB – Connecting Database



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command on a database if the database doesn”t exist then MongoDB creates it automatically.

    // Create a Command Instance
    $statistics = new MongoDBDriverCommand(["dbstats" => 1]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("myDb", $statistics);
    

    Example

    Try the following example to connect to a MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          echo "Connection to database successfully";
          $statistics = new MongoDBDriverCommand(["dbstats" => 1]);
          $cursor = $manager->executeCommand("myDb", $statistics);
          $statistics = current($cursor->toArray());
          echo "<pre>";
          print_r($statistics);
          echo "</pre>";
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    Connection to database successfully
    stdClass Object
    (
       [db] => myDb
       [collections] => 0
       [views] => 0
       [objects] => 0
       [avgObjSize] => 0
       [dataSize] => 0
       [storageSize] => 0
       [totalSize] => 0
       [indexes] => 0
       [indexSize] => 0
       [scaleFactor] => 1
       [fileSize] => 0
       [fsUsedSize] => 0
       [fsTotalSize] => 0
       [ok] => 1
    )
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Display Collections nhận dự án làm có lương

    PHP & MongoDB – Display Collections



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command on a database to show the list of collections available.

    // Create a Command Instance
    $collectionList = new MongoDBDriverCommand(["listCollections" => 1]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("myDb", $collectionList);
    

    Example

    Try the following example to list collections of a database in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          // Create a Command Instance
          $collectionList = new MongoDBDriverCommand(["listCollections" => 1]);
    
          // Execute the command on the database
          $cursor = $manager->executeCommand("myDb", $collectionList);
    
          $collections = $cursor->toArray();
    
          foreach ($collections as $collection) {
             echo $collection->name . "<br/>";
          }
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    sampleCollection
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MongoDB – Show Databases nhận dự án làm có lương

    PHP & MongoDB – Show Databases



    First step to do any operation is to create a Manager instance.

    // Connect to MongoDB using Manager Instance
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    Second step is to prepare and execute a command on a database to show the list of databases available.

    // Create a Command Instance
    $databaseList = new MongoDBDriverCommand(["listDatabases" => 1]);
    
    // Execute the command on the database
    $cursor = $manager->executeCommand("admin", $databaseList);
    

    Example

    Try the following example to list databases available by default in MongoDB server −

    Copy and paste the following example as mongodb_example.php −

    <?php
       try {
          // connect to mongodb
          $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
          // Create a Command Instance
          $databaseList = new MongoDBDriverCommand(["listDatabases" => 1]);
    
          // Execute the command on the database
          $cursor = $manager->executeCommand("admin", $databaseList);
    
          $databases = current($cursor->toArray());
    
          foreach ($databases->databases as $database) {
             echo $database->name . "<br/>";
          }
       } catch (MongoDBDriverExceptionException $e) {
          echo "Exception:", $e->getMessage(), "n";
       }
    ?>
    

    Output

    Access the mongodb_example.php deployed on apache web server and verify the output.

    admin
    config
    local
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc