0% found this document useful (0 votes)
64 views7 pages

PHP With Mongodb

This document provides code examples for connecting to a MongoDB database, selecting a database, creating a collection, inserting documents, finding documents, updating documents, and deleting documents. The code connects to the MongoDB database, selects the examplesdb database, creates a collection called examplescol, inserts a sample document, finds all documents in the collection, updates a document's name field, and deletes a document matching certain criteria.

Uploaded by

Madhushree S G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views7 pages

PHP With Mongodb

This document provides code examples for connecting to a MongoDB database, selecting a database, creating a collection, inserting documents, finding documents, updating documents, and deleting documents. The code connects to the MongoDB database, selects the examplesdb database, creates a collection called examplescol, inserts a sample document, finds all documents in the collection, updates a document's name field, and deletes a document matching certain criteria.

Uploaded by

Madhushree S G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Make a Connection and Select a Database


For connecting to the MongoDB database you need to specify the name of
the database, if the database does not exist then MongoDB will create it
automatically.
Following is the code for that:
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->examplesdb;
echo "Database examplesdb selected";
?>
When you execute the above program, it will display the following result:
Connection to the database successfully
Database examplesdb selected
2. Create a Collection
Following is the code for creating a collection in MongoDB:
<?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";
?>
This is the output for the code:
Connection to the database successfully
Database examplesdb selected
Collection created successfully
Following is the code for inserting 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";
?>
After executing the code you will get the following output:
Connection to the database successfully
Database examplesdb selected
Collection selected successfully
Document inserted successfully
 Find All Documents
find() method is used to select all documents from the collection.
Following is the code to 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["title"] . "\n";
}
?>
Update a Document
update() method is used to update a document in MongoDB.
Following is the code to 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
remove() method is used to delete a document in MongoDB.
Following is the code to 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";
}
?>

You might also like