YouTip LogoYouTip

Mongodb Php

To use MongoDB in PHP, you must use the MongoDB PHP driver. For installation of MongoDB PHP on various platforms and driver package downloads, please see: (#) If you are using PHP7, please refer to: (#). ## Ensure Connection and Select a Database To ensure a correct connection, you need to specify the database name. If the database does not exist in MongoDB, MongoDB will automatically create it. The code snippet is as follows: test; // Get the database named "test"?> * * * ## Create a Collection The code snippet for creating a collection is as follows: test; // Get the database named "test" $collection = $db->createCollection("tutorial"); echo "Collection created successfully";?> Executing the above program, the output is as follows: Collection created successfully * * * ## Insert a Document In MongoDB, use the insert() method to insert a document: The code snippet for inserting a document is as follows: test; // Select a database $collection = $db->tutorial; // Select a collection $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100,"url" => "","by", "Tutorial"); $collection->insert($document); echo "Data inserted successfully";?> Executing the above program, the output is as follows: Data inserted successfully Then we use the **db.tutorial.find().pretty();** command in the mongo client to view the data: !(#) * * * ## Find Documents Use the find() method to read documents in a collection. The code snippet for reading documents is as follows: test; // Select a database $collection = $db->tutorial; // Select a collection $cursor = $collection->find();// Iterate and display document titles foreach ($cursor as $document) {echo $document . "n";}?> Executing the above program, the output is as follows: MongoDB * * * ## Update a Document Use the update() method to update a document. The following example updates the title in the document to 'MongoDB Tutorial'. The code snippet is as follows:
test; // Select a database $collection = $db->tutorial; // Select a collection// Update the document $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB Tutorial")));// Display the updated document $cursor = $collection->find();// Loop to display document titles foreach ($cursor as $document) {echo $document . "n";}?>
Executing the above program, the output is as follows:

MongoDB Tutorial
Then we use the **db.tutorial.find().pretty();** command in the mongo client to view the data:

!(#)

* * *

## Delete a Document

Use the remove() method to delete a document.

In the following example, we will remove a data record where the 'title' is 'MongoDB Tutorial'. The code snippet is as follows:

test; // Select a database $collection = $db->tutorial; // Select a collection // Remove the document $collection->remove(array("title"=>"MongoDB Tutorial"), array("justOne" => true));// Display available document data $cursor = $collection->find();foreach ($cursor as $document) {echo $document . "n";}?>
In addition to the above examples, you can also use methods like findOne(), save(), limit(), skip(), sort(), etc., to operate on the MongoDB database in PHP.

For more operation methods, please refer to the MongoDB core class: [http://php.net/manual/zh/mongo.core.php](http://php.net/manual/zh/mongo.core.php).
← Java CollectionsMongodb Java β†’