l1n6yun's Blog

记录学习的技能和遇到的问题

0%

PHP 调用 MongoDB 全解析

PHP 调用 MongoDB 全解析

MongoDB 是一款流行的开源文档型数据库,它以 JSON 风格的文档形式存储数据,具有高性能、高可扩展性和灵活的数据模型等特点。而 PHP 作为一种广泛应用于 Web 开发的脚本语言,与 MongoDB 结合可以为开发者提供强大的数据存储和处理能力。本文将详细介绍如何使用 PHP 调用 MongoDB 进行数据的增删改查等操作。

环境准备

在使用 PHP 调用 MongoDB 之前,需要确保已经安装了 MongoDB 数据库和 PHP 的 MongoDB 扩展。可以使用 Composer 来安装 MongoDB 的 PHP 驱动,在项目根目录下创建一个composer.json文件,内容如下:

json

1
2
3
4
5
{
"require": {
"mongodb/mongodb": "^1.13"
}
}

然后在终端中执行composer install命令来安装依赖。

代码示例与解析

连接 MongoDB

1
2
3
4
5
6
7
8
9
10
11
12
<?php

require 'vendor/autoload.php';

$client = new MongoDB\Client("mongodb://localhost:27017");
//指定数据库
$db = $client->selectDatabase('test');
//指定集合
$collection = $db->selectCollection('users');

// 另一种指定合集的方式
$collection = $client->test->users;

上述代码首先通过require 'vendor/autoload.php';引入 Composer 的自动加载文件,然后使用MongoDB\Client类来连接本地的 MongoDB 服务器,端口为27017。接着指定了要使用的数据库test和集合users

插入数据

插入单条数据

1
2
3
4
5
$collection->insertOne([
'name' => '张三',
'age' => 18,
'sex' => '男'
]);

使用insertOne方法可以向集合中插入一条数据,数据以关联数组的形式传入。

指定 ID 插入数据

1
2
3
4
5
6
$collection->insertOne([
'_id' => new MongoDB\BSON\ObjectID('5e7b0b0b0b0b0b0b0b0b0b0b'),
'name' => '李四',
'age' => 27,
'sex' => '男'
]);

在插入数据时,可以手动指定_id字段,使用MongoDB\BSON\ObjectID类来创建一个 ObjectID 对象。

插入多条数据

1
2
3
4
5
6
7
8
9
10
11
12
$collection->insertMany([
[
'name' => '王五',
'age' => 18,
'sex' => '男'
],
[
'name' => '赵六',
'age' => 18,
'sex' => '男'
]
]);

使用insertMany方法可以一次性插入多条数据,传入一个二维数组,每个子数组代表一条数据。

查询数据

查询单条数据

1
2
3
4
$document = $collection->findOne([
'name' => '张三'
]);
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));

使用findOne方法可以查询符合条件的第一条数据,返回一个文档对象,使用json_encode方法将其转换为 JSON 字符串并输出。

查询多条数据

1
2
3
4
5
6
$cursor = $collection->find([
'name' => '张三'
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

使用find方法可以查询符合条件的多条数据,返回一个游标对象,通过foreach循环遍历游标对象,输出每条数据。

查询指定数量的数据

1
2
3
4
5
6
7
8
$cursor = $collection->find([
'name' => '张三'
], [
'limit' => 2
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

find方法的第二个参数中,可以使用limit选项来指定查询结果的数量。

条件查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 查询年龄大于18的数据
$cursor = $collection->find([
'age' => [
'$gt' => 18
]
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

// 查询年龄小于18,名称等于张三的数据
$cursor = $collection->find([
'name' => '张三',
'age' => [
'$lt' => 19
]
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

// 查询性别为空的数据
$cursor = $collection->find([
'sex' => [
'$exists' => false
]
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

在查询条件中,可以使用 MongoDB 的查询操作符,如$gt(大于)、$lt(小于)、$exists(是否存在)等。

排序查询

1
2
3
4
5
6
7
8
9
10
$cursor = $collection->find([
'name' => '张三'
], [
'sort' => [
'age' => 1 // 1正序,-1倒叙
]
]);
foreach ($cursor as $document) {
var_dump(json_encode($document, JSON_UNESCAPED_UNICODE));
}

find方法的第二个参数中,可以使用sort选项来对查询结果进行排序,1表示正序,-1表示倒序。

修改数据

查询并修改

1
2
3
4
5
6
7
8
$cursor = $collection->findOneAndUpdate([
'name' => '张三'
], [
'$set' => [
'age' => 27
]
]);
var_dump(json_encode($cursor, JSON_UNESCAPED_UNICODE));

使用findOneAndUpdate方法可以查询符合条件的第一条数据并进行修改,返回修改前的文档对象。

更新数据

1
2
3
4
5
6
7
$collection->updateOne([
'name' => '张三'
], [
'$set' => [
'age' => 27
]
]);

使用updateOne方法可以更新符合条件的第一条数据,使用$set操作符来指定要更新的字段和值。

查询并替换

1
2
3
4
5
6
7
$cursor = $collection->findOneAndReplace([
'name' => '张三'
], [
'name' => '李四',
'age' => 27
]);
var_dump(json_encode($cursor, JSON_UNESCAPED_UNICODE));

使用findOneAndReplace方法可以查询符合条件的第一条数据并进行替换,返回替换前的文档对象。

存在更新不存在插入

1
2
3
4
5
6
7
8
9
$collection->updateOne([
'name' => '张三'
], [
'$set' => [
'age' => 27
]
], [
'upsert' => true
]);

updateOne方法的第三个参数中,使用upsert选项设置为true,可以实现如果符合条件的数据存在则更新,不存在则插入的功能。

更新多条数据

1
2
3
4
5
6
7
$collection->updateMany([
'name' => '张三'
], [
'$set' => [
'age' => 27
]
]);

使用updateMany方法可以更新符合条件的多条数据。

删除数据

删除单条数据

1
2
3
$collection->deleteOne([
'name' => '张三'
]);

使用deleteOne方法可以删除符合条件的第一条数据。

删除多条数据

1
2
3
$collection->deleteMany([
'name' => '张三'
]);

使用deleteMany方法可以删除符合条件的多条数据。

删除集合和数据库

1
2
3
4
5
// 删除集合
$collection->drop();

// 删除数据库
$db->drop();

使用drop方法可以删除集合和数据库。