Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB PHP

MongoDB PHP

To use mongodb in php, you must use the mongodb php driver.

For MongoDB PHP installation and driver package downloads on various platforms, please see:PHP installation of the MongoDB extension driver

If you are using PHP7, please refer to:PHP7 MongoDB installation and usage。

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 create it automatically.

The code snippet is as follows:



test; // 获取名称为 "test" 的数据库

?>


Create a collection

The code snippet for creating a collection is as follows:



test; // 获取名称为 "test" 的数据库

$collection = $db->createCollection("runoob");

echo "集合创建成功";

?>

Running the above program produces the following output:



集合创建成功


Insert documents

Use the insert() method to insert a document in mongoDB:

The code snippet for inserting a document is as follows:



test;            // 选择一个数据库

$collection = $db->runoob; // 选择集合

$document = array( 

    "title" => "MongoDB", 

    "description" => "database", 

    "likes" => 100,

    "url" => "http://www.runoob.com/mongodb/",

    "by", "Runoob Tutorial"

);

$collection->insert($document);

echo "数据插入成功";

?>

Running the above program produces the following output:



数据插入成功

Then we use in the mongo client db.runoob.find().pretty(); command 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;            // 选择一个数据库

$collection = $db->runoob; // 选择集合



$cursor = $collection->find();

// Iterate and display document titles

foreach ($cursor as $document) {

    echo $document["title"] . "\n";

}

?>

Running the above program produces the following output:



MongoDB


Update documents

Use the update() method to update documents.

The following example will update the title in the document to 'MongoDB Tutorial'; the code snippet is as follows:




test;            // 选择一个数据库

$collection = $db->runoob; // 选择集合

// Update documentation

$collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));

// Show updated documentation

$cursor = $collection->find();

// Loop to display document title

foreach ($cursor as $document) {

    echo $document["title"] . "\n";

}

?>

Running the above program produces the following output:



MongoDB 教程

Then we use in the mongo client db.runoob.find().pretty(); command to view the data:


Delete documents

Use the remove() method to delete a document.

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



test;            // 选择一个数据库

$collection = $db->runoob; // 选择集合

   

// Remove documentation

$collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));



// Display available document data

$cursor = $collection->find();

foreach ($cursor as $document) {

    echo $document["title"] . "\n";

}

?>

In addition to the examples above, in php you can also use methods such as findOne(), save(), limit(), skip(), and sort() to operate on the Mongodb database.

For more operation methods, refer to the Mongodb core classes:http://php.net/manual/zh/mongo.core.php。