PHP操作MongoDB的例子
PHP操作MongoDB的例子
要将MongoDB与PHP结合使用,您需要使用MongoDB PHP驱动程序。从https://s3.amazonaws.com/drivers.mongodb.org/php/index.html下载驱动程序下载PHP驱动程序。请确保下载最新的版本。现在解压缩归档文件,并将php_mongo.dll放在PHP扩展目录(默认情况下为“ext”)中,并将以下行添加到PHP.ini文件中。
extension = php_mongo.dll
建立连接并选择数据库
要建立连接,需要指定数据库名称,如果数据库不存在,MongoDB将自动创建它。
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; ?>
执行程序时,它将产生以下结果:
Connection to database successfully Database mydb selected 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"; ?>
执行程序时,它将产生以下结果:
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->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"; ?>
执行程序时,它将产生以下结果:
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->mycol; echo "Collection selected succsessfully"; $cursor = $collection->find(); // iterate cursor to display title of documents foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
执行程序时,它将产生以下结果:
Connection to database successfully Database mydb selected Collection selected succsessfully { "title": "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 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"; } ?>
执行程序时,它将产生以下结果:
Connection to database successfully Database mydb selected Collection selected succsessfully Document updated successfully Updated document { "title": "MongoDB Tutorial" }
删除文档
要删除文档,需要使用remove()方法。
<?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"; } ?>
执行程序时,它将产生以下结果:
Connection to database successfully Database mydb selected Collection selected succsessfully Documents deleted successfully
在上面的示例中,第二个参数是布尔类型,仅用于remove()方法的一个字段。
其余MongoDB方法如findOne()、save()、limit()、skip()、sort()等。工作原理与上述说明相同。