Steps of Mongodb with PHP Connectivity.


Mongodb with PHP Connectivity







Make a Connection and Select a Database
<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";
  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
?>

Create a Collection
<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";
  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->createCollection("examplescol");
  echo "Collection created succsessfully";
?>
Insert a Document
<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";
  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->examplescol;
  echo "Collection selected succsessfully";
  $document = array(
  "title" => "MongoDB",
  "description" => "database",
  "likes" => 100,
  "url" => "http://www.data-flair.training/mongodb/",
  "by" => "data flair"
 );
$collection->insert($document);
echo "Document inserted successfully";
?>
Find All Documents
<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";
  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->examplescol;
  echo "Collection selected succsessfully";
  $cursor = $collection->find();
  // iterate cursor to display title of documents
  foreach ($cursor as $document) {
  echo $document["name"] . "\n";
 }
?>
Update a Document
<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
   // select a database
   $db = $m->examplesdb;
   echo "Database examplesdb selected";
   $collection = $db->examplescol;
   echo "Collection selected succsessfully";
   // now update the document
   $collection->update(array("name"=>"MongoDB"),
   array('$set'=>array("name"=>"MongoDB Tutorial")));
   echo "Document updated successfully";
   // now display the updated document
   $cursor = $collection->find();
   // iterate cursor to display title of documents
   echo "Updated document";
   foreach ($cursor as $document) {
   echo $document["name"] . "\n";
 }
?>
Delete a Document
<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
   // select a database
   $db = $m->examplesdb;
   echo "Database examplesdb selected";
   $collection = $db->examplescol;
   echo "Collection selected succsessfully";
   // now remove the document
   $collection->remove(array("name"=>"MongoDB Tutorial"),false);
   echo "Documents deleted successfully";
   // now display the available documents
   $cursor = $collection->find();
   // iterate cursor to display title of documents
   echo "Updated document";
   foreach ($cursor as $document) {
   echo $document["name"] . "\n";
 }
?>

Comments

Popular posts from this blog

How to easily upgrade React Native and the project dependencies.

CodeIgniter PHP website into a PWA (progressive web app)!!!