在MongoDB中,您可以使用db.collection.updateOne()
方法更新集合中的单个文档。此方法根据过滤器更新集合中的文档。
updateOne()
是mongo shell方法,可以一次更新一个文档。此方法更新满足集合中给定条件的第一个文档。换句话说,如果多个文档满足给定条件,则此方法将更新与给定过滤器或条件匹配的第一个文档。
-
updateOne()
方法仅接受包含更新运算符表达式的文档。 - 此方法可用于多文档交易中。
- 更新文档时,_id字段的值保持不变。
- 它还在文档中添加了新字段。
Syntax:
Parameters:
filter: First parameter of this method. It specifies the selection criteria for the update. The type of this parameter is document. If it contains empty document, i.e, {}, then this method will update the first document of the collection with the update document.
update: Second parameter of this method. The type of this parameter is document or pipeline and it contains modification that will apply to the document. It can be a update Document(only contain update operator expressions) or aggregation pipeline(only contain aggregation stages, i.e, $addFields
, $project
, $replaceRoot
).
Optional Parameters:
- upsert: The value of this parameter is either true or false. If the value of this parameter is true, then the method will update the document that matches the given condition or if any of the documents in the collection does not match the given filter, then this method will insert a new document(i.e., update Document) in the collection. The type of this parameter is a Boolean and the default value of this parameter is false.
- writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is document.
- collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is document.
- arrayFilters: It is an array of filter documents that indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.
- hint: It is a document or field that specifies the index to use to support the filter. It can take an index specification document or the index name string and if you specify an index that does not exist, then it will give an error.
Return: This method will return a document that contains a boolean acknowledged as true (if the write concern is enabled) or false (if the write concern is disabled), matchedCount represents the number of matched documents, modifiedCount represents the number of modified documents, and upsertedId represents the _id of the updated document.
例子:
在以下示例中,我们正在使用:
db.collection.updateOne(
,
,
{
upsert: ,
writeConcern: ,
collation: ,
arrayFilters: [ , ... ],
hint:
}
)
更新第一个文件:
在此示例中,我们正在使用db.collection.updateOne()
方法更新雇员集合的第一个文档中的Department字段的值。换句话说,我们更新了Sonu的部门。因此,在更新Sonu部门之前为“开发”,而在更新Sonu部门之后为“ HR”。
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
在这里, $set
是一个更新运算符,它用于更新部门字段的值。
更新与过滤器匹配的单个文档:
在此示例中,我们将更新员工集合中与给定过滤器匹配的单个文档,即分支:“ ECE”。换句话说,将部门更新为分支机构为ECE的员工的“开发”。
更新文件:
在此示例中,我们正在更新员工集合中的文档。这里有多个文档与给定的过滤器匹配,因此此方法将更新这些文档中的第一个文档,如下图所示:
更新文档中的多个字段:
在此示例中,我们将更新单个文档( "_id" : ObjectId("5e49813692e6dfa3fc48dd74")
)中的多个字段。