Docs Home / Node.js Driver

Compound Operations复合操作

Overview概述

Most database requests either read data from a database or write data into a database. However, there are instances where you may require a single operation that reads and writes data.大多数数据库请求要么从数据库读取数据,要么将数据写入数据库。但是,在某些情况下,您可能需要一个读取和写入数据的操作。

Compound operations combine read and write operations in a single atomic statement, so there's no chance of data changing in between a read and a subsequent write.复合操作将读取和写入操作组合在一个原子语句中,因此在读取和后续写入之间数据不会发生变化。

If you execute each operation separately, another request may alter the data between the read and write operations. These data changes may not prevent your operation from succeeding, but they can make error handling more difficult. 如果分别执行每个操作,则另一个请求可能会更改读操作和写操作之间的数据。这些数据更改可能不会阻止您的操作成功,但会使错误处理更加困难。When your application handles potential errors at any stage of the process, it can become brittle and difficult to test.当您的应用程序在流程的任何阶段处理潜在错误时,它可能会变得脆弱且难以测试。

Built-in Methods内置方法

The Node.js driver provides the following methods to perform compound operations:Node.js驱动程序提供以下方法来执行复合操作:

These methods accept an optional options object with configurable sort and projection options.这些方法接受具有可配置sortprojection选项的可选options对象。

You can also set the includeResultMetadata option to specify the return type of each of these methods. To learn more about this option, see the includeResultMetadata Option section of this guide.您还可以设置includeResultMetadata选项来指定这些方法的返回类型。要了解有关此选项的更多信息,请参阅本指南的includeResultMetadata选项部分。

The findOneAndUpdate() and findOneAndDelete() methods take the returnDocument setting, which specifies if the method returns the pre-update or post-update version of the modified document.findOneAndUpdate()findOneAndDelete()方法采用returnDocument设置,该设置指定该方法是返回修改文档的更新前版本还是更新后版本。

includeResultMetadata Option选项

The includeResultMetadata option determines the return type of the compound methods.includeResultMetadata选项决定了复合方法的返回类型。

This setting defaults to false, which means that each method returns the matched document. If no document is matched, each method returns null. 此设置默认为false,这意味着每个方法都返回匹配的文档。如果没有匹配的文档,则每个方法返回nullIf you set includeResultMetadata to true, the method returns a ModifyResult type that contains the found document and metadata.如果将includeResultMetadata设置为true,则该方法将返回包含找到的文档和元数据的ModifyResult类型。

Suppose a collection contains only the following document:假设一个集合只包含以下文档:

{ _id: 1, x: "on" }

The following table shows how the value of the includeResultMetadata option changes the return type of the findOneAndDelete() method:下表显示了includeResultMetadata选项的值如何更改findOneAndDelete()方法的返回类型:

Option Value选项值Syntax and Output语法和输出
Default: 默认值:false

Document matched文档匹配

await coll.findOneAndDelete({ x: "on" });
{ _id: 1, x: 'on' }

No document matched没有匹配的文档

await coll.findOneAndDelete({ x: "off" });
null
true
await coll.findOneAndDelete({ x: "on" }, { includeResultMetadata: true });
{ lastErrorObject: { n: 1 }, value: { _id: 1, x: 'on' }, ok: 1, ... }