3.29. MongoDB PHP

发布时间 :2025-10-25 12:32:58 UTC      

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:

<?php
$m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017
$db = $m->test; // 获取名称为 "test" 的数据库
?>

Create a collection

The code snippet to create the collection is as follows:

<?php
$m = new MongoClient(); // 连接
$db = $m->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:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->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:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->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:

<pre>
<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->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:

<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->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 .

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.