文档菜单
文档首页
/ / /
PHP库手册

将数据写入MongoDB

本页内容

  • 概述
  • 示例应用程序
  • 插入一个
  • 插入多个
  • 更新一个
  • 更新多个
  • 替换一个
  • 删除一个
  • 删除多个
  • 批量写
  • 存储大文件

在本页中,您可以查看可复制的代码示例,展示了MongoDB PHP库的常见方法,用于将数据写入MongoDB。

提示

要了解本页上显示的任何方法的更多信息,请参阅每个部分中提供的链接。

要使用本页上的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请确保将MONGODB_URI环境变量设置为MongoDB部署的连接字符串,并用您的目标命名空间值替换<database><collection>占位符。

您可以使用以下示例应用程序测试本页上的代码示例。要使用示例应用程序,请执行以下步骤

  1. 确保您已将MongoDB PHP库安装到项目中。有关安装MongoDB PHP库的更多信息,请参阅下载和安装指南。

  2. 复制以下代码并将其粘贴到新的.php文件中。

  3. 从本页复制代码示例并将其粘贴到文件中的指定行。

1<?php
2
3require __DIR__ . '/../vendor/autoload.php';
4
5$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6$client = new MongoDB\Client($uri);
7
8$collection = $client->selectCollection('<database>', '<collection>');
9
10// Start example code here
11
12// End example code here

以下代码演示了如何将单个文档插入到集合中

$result = $collection->insertOne([
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
]);

要了解更多关于 MongoDB\Collection::insertOne() 方法的知识,请参阅插入文档指南。

以下代码演示了如何将多个文档插入到集合中

$result = $collection->insertMany(
[
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
],
[
'<field name 1>' => '<value 1>',
'<field name 2>' => '<value 2>',
],
);

要了解更多关于 MongoDB\Collection::insertMany() 方法的知识,请参阅插入文档指南。

以下代码演示了如何通过创建或编辑字段来更新集合中的单个文档

$result = $collection->updateOne(
['<field to match>' => '<value to match>'],
['$set' => ['<field name>' => '<value>']],
);

要了解更多关于 MongoDB\Collection::updateOne() 方法的知识,请参阅更新文档指南。

以下代码展示了如何通过创建或编辑一个字段来更新集合中的多个文档

$result = $collection->updateMany(
['<field to match>' => '<value to match>'],
['$set' => ['<field name>' => '<value>']],
);

要了解更多关于 MongoDB\Collection::updateMany() 方法的知识,请参阅更新文档指南。

以下代码展示了如何用另一个文档替换集合中的一个文档

$result = $collection->replaceOne(
['<field to match>' => '<value to match>'],
[
'<new field 1>' => '<value 1>',
'<new field 2>' => '<value 2>',
],
);

要了解更多关于 MongoDB\Collection::replaceOne() 方法的知识,请参阅替换文档指南。

以下代码展示了如何删除集合中的一个文档

$result = $collection->deleteOne(['<field name>' => '<value>']);

要了解更多关于 MongoDB\Collection::deleteOne() 方法的知识,请参阅删除文档指南。

以下代码展示了如何在集合中删除多个文档

$result = $collection->deleteMany(['<field name>' => '<value>']);

要了解有关 MongoDB\Collection::deleteMany() 方法的更多信息,请参阅删除文档指南。

以下代码展示了如何在一个批量操作中执行多个写入操作

$result = $collection->bulkWrite(
[
[
'insertOne' => [
['<field name>' => '<value>'],
],
],
[
'replaceOne' => [
['<field to match>' => '<value to match>'],
[
'<first new field>' => '<value>',
'<second new field>' => '<value>',
],
],
],
[
'updateOne' => [
['<field to match>' => '<value to match>'],
['$set' => ['<field to update>' => '<value to update>']],
],
],
[
'updateMany' => [
['<field to match>' => '<value to match>'],
['$set' => ['<field to update>' => '<value to update>']],
],
],
[
'deleteOne' => [
['<field name>' => '<value>'],
],
],
[
'deleteMany' => [
['<field name>' => '<value>'],
],
],
]
);

要了解有关 MongoDB\Collection::bulkWrite() 方法的更多信息,请参阅批量写入指南。

以下代码展示了如何通过创建上传流将文件存储在 GridFS 存储桶中

$bucket = $client->selectDatabase('<database name>')->selectGridFSBucket();
$stream = $bucket->openUploadStream('<file name>');
fwrite($stream, '<data>');
fclose($stream);

要了解有关 GridFS 的更多信息,请参阅存储大文件指南。

返回

监控变更