PHP & MongoDB - Inserting Documents



Steps to Insert a Document in a collection

First step to do any operation is to create a MongoDB Client instance.

$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);

Second step is to prepare and execute a command to select a collection and insert document using insertOne() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define a Document
$document = [
   'First_Name' => 'Mahesh', 
   'Last_Name' => 'Parashar', 
   'Date_Of_Birth' => '1990-08-21',
   'e_mail' => 'mahesh_parashar.123@gmail.com',
   'phone' => '9034343345'];

// insert the document in the collection
$result = $collection->insertOne($document);

// Print result
printf("Inserted document ID: %s\n", $result->getInsertedId())

Example - Inserting a Document in a MongoDB Collection

Try the following example to insert a single document in a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Document
   $document = [
      'First_Name' => 'Mahesh', 
      'Last_Name' => 'Parashar', 
      'Date_Of_Birth' => '1990-08-21',
      'e_mail' => 'mahesh_parashar.123@gmail.com',
      'phone' => '9034343345'
   ];

   // insert the document in the collection
   $result = $collection->insertOne($document);

   // Print result
   printf("Inserted document ID: %s\n", $result->getInsertedId())
?>

Output

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

Inserted document ID: 696c20bffed319cd950058c4

Steps to Insert Multiple Documents in a collection

Prepare and execute a command to select a collection and insert documents using insertMany() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define Documents
$documents = [
   [
      'First_Name' => 'Mahesh', 
      'Last_Name' => 'Parashar', 
      'Date_Of_Birth' => '1990-08-21',
      'e_mail' => 'mahesh_parashar.123@gmail.com',
      'phone' => '9034343345'
   ],
   [
      'First_Name' => "Radhika", 
      'Last_Name' => 'Sharma', 
      'Date_Of_Birth' => '1995-09-26',
      'e_mail' => 'radhika_sharma.123@gmail.com',
      'phone' => '9000012345'
   ],
   [
      'First_Name' => "Rachel", 
      'Last_Name' => 'Christopher', 
      'Date_Of_Birth' => '1990-02-16',
      'e_mail' => 'rachel_christopher.123@gmail.com',
      'phone' => '9000054321'
   ],
   [
      'First_Name' => "Fathima", 
      'Last_Name' => 'Sheik', 
      'Date_Of_Birth' => '1990-02-16',
      'e_mail' => 'fathima_sheik.123@gmail.com',
      'phone' => '9000012345'
   ]
];

// insert the documents in the collection
$result = $collection->insertMany($documents);

// Print result
printf("Inserted %d document(s).\n", $result->getInsertedCount());

Example - Inserting a Document in a MongoDB Collection

Try the following example to insert a single document in a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define Documents
   $documents = [
      [
         'First_Name' => 'Mahesh', 
         'Last_Name' => 'Parashar', 
         'Date_Of_Birth' => '1990-08-21',
         'e_mail' => 'mahesh_parashar.123@gmail.com',
         'phone' => '9034343345'
      ],
      [
         'First_Name' => "Radhika", 
         'Last_Name' => 'Sharma', 
         'Date_Of_Birth' => '1995-09-26',
         'e_mail' => 'radhika_sharma.123@gmail.com',
         'phone' => '9000012345'
      ],
      [
         'First_Name' => "Rachel", 
         'Last_Name' => 'Christopher', 
         'Date_Of_Birth' => '1990-02-16',
         'e_mail' => 'rachel_christopher.123@gmail.com',
         'phone' => '9000054321'
      ],
      [
         'First_Name' => "Fathima", 
         'Last_Name' => 'Sheik', 
         'Date_Of_Birth' => '1990-02-16',
         'e_mail' => 'fathima_sheik.123@gmail.com',
         'phone' => '9000012345'
      ]
   ];

   // insert the documents in the collection
   $result = $collection->insertMany($documents);

   // Print result
   printf("Inserted %d document(s).\n", $result->getInsertedCount());
?>

Output

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

Inserted 4 document(s).
Advertisements