Mongodb 简明教程
MongoDB - PHP
要将 MongoDB 与 PHP 一起使用,您需要使用 MongoDB PHP 驱动程序。从 url Download PHP Driver 下载该驱动程序。务必下载其最新版本。现在解压存档并将 php_mongo.dll 放在你的 PHP 扩展目录(默认情况下为 “ext”)中,并在你的 php.ini 文件中添加以下行:
To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver. Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory ("ext" by default) and add the following line to your php.ini file −
extension = php_mongo.dll
Make a Connection and Select a Database
为了连接,您需要指定数据库名称,如果数据库不存在,则 MongoDB 会自动创建它。
To make a connection, you need to specify the database name, if the database doesn’t exist then MongoDB creates it automatically.
以下是连接数据库的代码片段:
Following is the code snippet to connect to the database −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Create a Collection
以下是创建集合的代码片段 −
Following is the code snippet to create a collection −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->createCollection("mycol");
echo "Collection created succsessfully";
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection created succsessfully
Insert a Document
要将文档插入到 MongoDB 中,使用 insert() 方法。
To insert a document into MongoDB, insert() method is used.
以下是插入文档的代码片段:
Following is the code snippet to insert a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://www.tutorialspoint.com/mongodb/",
"by" => "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully
Find All Documents
要从集合中选择所有文档,使用 find() 方法。
To select all documents from the collection, find() method is used.
以下是选择所有文档的代码片段:
Following is the code snippet to select all documents −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully {
"title": "MongoDB"
}
Update a Document
要更新文档,需要使用 update() 方法。
To update a document, you need to use the update() method.
在以下示例中,我们将会把已插入文档的标题更新为 MongoDB Tutorial 。以下是更新文档的代码片段:
In the following example, we will update the title of inserted document to MongoDB Tutorial. Following is the code snippet to update a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
// now update the document
$collection->update(array("title"=>"MongoDB"),
array('$set'=>array("title"=>"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["title"] . "\n";
}
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
"title": "MongoDB Tutorial"
}
Delete a Document
要删除文档,需要使用 remove() 方法。
To delete a document, you need to use remove() method.
在以下示例中,我们将移除标题为 MongoDB Tutorial 的文档。以下是删除文档的代码片段:
In the following example, we will remove the documents that has the title MongoDB Tutorial. Following is the code snippet to delete a document −
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
// now remove the document
$collection->remove(array("title"=>"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["title"] . "\n";
}
?>
程序执行后,将产生以下结果:
When the program is executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully
在上述示例中,第二个参数是布尔类型,并用于 remove() 方法的 justOne 字段。
In the above example, the second parameter is boolean type and used for justOne field of remove() method.
剩余的 MongoDB 方法 findOne(), save(), limit(), skip(), sort() 等同于上述解释。
Remaining MongoDB methods findOne(), save(), limit(), skip(), sort() etc. works same as explained above.