Docs 菜单
Docs 主页
/ / /
Ruby 驱动程序
/

替换文档

在本指南中,您可以学习;了解如何使用Ruby驾驶员对MongoDB集合中的文档执行替换操作。替换操作会删除指定文档中除_id 字段之外的所有字段和值,并添加您指定的新字段和值。此操作与更新操作不同,更新操作仅更改一个或多个文档中的指定字段。

要学习;了解有关更新操作的更多信息,请参阅更新文档指南。

本指南中的示例使用restaurants sample_restaurantsAtlas示例数据集的 数据库中的Mongo::Client 集合。要从Ruby应用程序访问权限此集合,请创建一个连接到Atlas 集群的 对象,并将以下值分配给databasecollection 变量:

database = client.use('sample_restaurants')
collection = database[:restaurants]

要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。

您可以使用 replace_one 方法在MongoDB中执行替换操作。 此方法会从与指定查询过滤匹配的第一个文档中删除除 _id字段之外的所有字段。 然后,它将您指定的字段和值添加到空文档中。

您必须将以下参数传递给replace_one方法:

  • 查询过滤:指定要更新的文档。如需学习;了解有关查询筛选器的更多信息,请参阅“指定查询”指南。

  • 替换文档:指定要替换现有字段和值的字段和值。

以下示例使用 replace_one 方法替换 name字段值为 "Primola Restaurant" 的文档的字段和值:

filter = { name: 'Primola Restaurant' }
new_document = { name: 'Frutti Di Mare', cuisine: 'Seafood', borough: 'Queens' }
result = collection.replace_one(filter, new_document)
puts "Replaced #{result.modified_count} document(s)"
Replaced 1 document(s)

重要

_id字段的值不可变。 如果替换文档为 _id字段指定了值,则该值必须与现有文档的 _id 值相同,否则驾驶员将引发 WriteError

您可以将 Hash对象作为参数传递给 replace_one 方法,以设立配置替换操作的选项。如果不指定任何选项,驾驶员将使用默认设置执行替换操作。

下表描述了可用于配置替换操作的选项:

选项
说明

upsert

Specifies whether the replace operation performs an upsert operation if no documents match the query filter. For more information, see upsert behavior in the MongoDB Server manual.
Defaults to false.

bypass_document_validation

Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
Defaults to false.

collation

Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.

session

Specifies the session to use for the operation. To learn more about sessions, see Client Sessions and Causal Consistency Guarantees in the MongoDB Server manual.

hint

Sets the index to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.

let

Provides a map of parameter names and values to set top-level variables for the operation. Values must be constant or closed expressions that don't reference document fields.

以下代码执行与前面的示例相同的替换操作,但将 upsert 选项设置为 true。如果查询过滤与任何现有文档都不匹配,则这会指示驾驶员插入具有替换文档中指定的字段和值的新文档:

options = { upsert: true }
result = collection.replace_one(filter, new_document, options)
puts "Replaced #{result.upserted_count} document(s)"
Replaced 1 document(s)

replace_one 方法返回一个 Mongo::Operation::Update::Result对象。您可以使用以下方法访问权限Result实例中的信息:

方法
说明

matched_count

Returns the number of documents that matched the query filter.

modified_count

Returns the number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.

upserted_count

Returns the number of documents upserted.

upserted_id

Returns the _id value of the document that the driver upserted into the database, if any.

要查看演示如何替换文档的可运行代码示例,请参阅将数据写入MongoDB。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档:

后退

插入文档

在此页面上