3.29. MongoDB PHP

发布时间 : 2025-10-25 13:34:22 UTC      

Page Views: 10 views

To use mongodb in php, you must use mongodb’s php driver.

For the installation of MongoDB PHP on each platform and the download of the driver package, please see: PHP安装MongoDB扩展驱动

If you are using PHP7, please see: PHP7 MongoDB 安装与使用 .

3.29.1. Make sure you connect 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; // 获取名称为 "test" 的数据库 ?> 

Create a collection

The code snippet to create the collection is as follows:

test; // 获取名称为 "test" 的数据库 $collection = $db->createCollection("runoob"); echo "集合创建成功"; ?> 

Execute the above procedure, and the output is as follows:

集合创建成功 

Insert document

Use in mongoDB insert() Method to insert the document:

Insert the document code snippet as follows:

test; // 选择一个数据库 $collection = $db->runoob; // 选择集合 $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.runoob.com/mongodb/", "by", "菜鸟教程" ); $collection->insert($document); echo "数据插入成功"; ?> 

Execute the above procedure, and the output is as follows:

数据插入成功 

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

Find a document

Use find() Method to read the documents in the collection.

The code snippet that reads the usage document is as follows:

test; // 选择一个数据库 $collection = $db->runoob; // 选择集合 $cursor = $collection->find(); // 迭代显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?> 

Execute the above procedure, and the output is as follows:

MongoDB 

Update document

Use update() Method to update the document.

The following example updates the document titled ‘MongoDB tutorial’ with the following code snippet:

 test; // 选择一个数据库 $collection = $db->runoob; // 选择集合 // 更新文档 $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程"))); // 显示更新后的文档 $cursor = $collection->find(); // 循环显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?> 

Execute the above procedure, and the output is as follows:

MongoDB 教程 

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

Image0

Delete document

Use remove() Method to delete the document.

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

test; // 选择一个数据库 $collection = $db->runoob; // 选择集合 // 移除文档 $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true)); // 显示可用文档数据 $cursor = $collection->find(); foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?> 

除了以上实例外,在php中你还可以使用 findOne() , save() , limit() , skip() , sort() 等方法来操作Mongodb数据库。

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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。