要使用php与mongodb交互存储数据,需要使用mongodb php驱动程序(http://pecl.php.net/package/mongo)。 从url下载驱动程序下载php驱动程序并确保下载的是正确的版本(如在本示例中:win10 64位下载的版本是:php_mongo-1.6.8-5.6-ts-vc11-x64.zip
)。 现在解压缩存档并将php_mongo.dll
放入php扩展目录(默认为“ext
”),并将以下行添加到php.ini
文件中 -
extension = php_mongo.dll
然后重新启动 apache 服务器,查看 phpinfo()
函数的输出结果 -
要进行连接,需要指定数据库名称,如果数据库不存在,那么mongodb会自动创建它。
以下是连接到数据库的代码片段 -
<?php
// connect to mongodb
$m = new mongoclient();
echo "connection to database successfully<br/>";
// select a database
$db = $m->mydb;
echo "database mydb selected";
?>
执行程序时,会产生以下结果 -
connection to database successfully
database mydb selected
以下是创建集合的代码片段 -
<?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("phpcol");
echo "collection created succsessfully";
?>
执行程序时,会产生以下结果 -
connection to database successfully
database mydb selected
collection created succsessfully
要将文档插入到mongodb中,请使用insert()
方法。
以下是插入文档的代码片段 -
<?php
// connect to mongodb
$m = new mongoclient();
echo "connection to database successfully";
// select a database
$db = $m->mydb;
echo "database mydb selected";
$collection = $db->phpcol;
echo "collection selected succsessfully";
$document = array(
"title" => "mongodb",
"description" => "database",
"likes" => 100,
"url" => "http://www.h3.com/mongodb/",
"by", "tutorials point"
);
$collection->insert($document);
echo "document inserted successfully";
?>
执行程序时,会产生以下结果 -
connection to database successfully
database mydb selected
collection selected succsessfully
document inserted successfully
要从集合中选择所有文档,请使用find()
方法。
<?php
// connect to mongodb
$m = new mongoclient();
echo "connection to database successfully";
// select a database
$db = $m->mydb;
echo "database mydb selected";
$collection = $db->phpcol;
echo "collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document["title"]. ', url is=> ' .$document["url"] . "
";
}
?>
以下是选择所有文档的代码片段 -
connection to database successfully
database mydb selected
collection selected succsessfully
mongodb, url is=> http://www.h3.com/mongodb/
要更新文档,需要使用update()
方法。
在下面的例子中,将把插入的文档的标题更新为:mongodb教程 。 以下是更新文档的代码段 -
<?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教程")));
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"] .', url => '.$document["url"] . "
";
}
?>
以下是选择所有文档的代码片段 -
connection to database successfully
database mydb selected
collection selected succsessfully
mongodb, url is=> http://www.h3.com/mongodb/
要删除文档,可使用remove()
方法。
在下面的示例中,将删除标题为:mongodb教程 的文档。 以下是删除文档的代码片段 -
<?php
// connect to mongodb
$m = new mongoclient();
echo "connection to database successfully";
// select a database
$db = $m->mydb;
echo "database mydb selected";
$collection = $db->phpcol;
echo "collection selected succsessfully";
// now remove the document
$collection->remove(array("title"=>"mongodb教程"),false);
echo "documents deleted successfully";
// now display the available documents
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["title"] . "
";
}
?>
以下是选择所有文档的代码片段 -
connection to database successfully
database mydb selected
collection selected succsessfully
documents deleted successfully
在上面的例子中,第二个参数是布尔类型,用于remove()
方法的justone
字段。
其余的mongodb方法findone()
,save()
,limit()
,skip()
,sort()
等与上述相同。
由于此篇教程文章是一个入门级的教程文章,只是讲解有关简单入门的操作,有关更高级的内容,请参考:http://docs.mongodb.com/php-library/