在MongoDB中,允许您借助db.collection.replaceOne()
方法将现有文档替换为集合中的新文档。此方法将用替换文档替换现有文档。
replaceOne()
是一种mongo shell方法,一次只能替换一个文档。与原始文档相比,替换文档可能包含不同的字段。
- 我们知道_id字段是不可变的,因此您可以在替换文档中省略_id字段。并且,如果要在替换文档中添加_id字段,则_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 replace the first document of the collection with the replacement document.
replacementDocument: Second parameter of this method. It is a replacement document that will replace the original document. It does not contain update operations.
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 replace the document that matches the given condition with the replacement document 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., replacementDocument) 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.
- 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 return a document that contain a boolean acknowledged as true (if the write concern is enabled) or false (if the write concern is disabled), matchedCount represents the total number of matched documents, modifiedCount represents the total number of modified documents, and upsertedId represents the _id of the upserted document. The upsertedId only appears in this document when the value of upsert parameter is set to true.
例子:
在以下示例中,我们正在使用:
db.collection.replaceOne(
,
,
{
upsert: ,
writeConcern: ,
collation: ,
hint:
}
)
替换第一个文档:
在此示例中,我们将用替换文档替换雇员集合的第一个文档,即{name: "Rohit", age: 20, branch: "CSE", department: "HR"}
,即{name: "Anu", age: 30, branch: "EEE", department: "HR", joiningYear: 2018}
使用replaceOne()
方法。
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
替换与过滤器匹配的单个文档:
在此示例中,我们将替换给定条件或过滤器的雇员集合的文档(即name: "Sonu"
替换为替换文档,即{name: "Sonu", age: 25, branch: "CSE", department: "Designing"}
使用replaceOne()
方法。换句话说,在此示例中,我们将替换名为Sonu的员工的文档。
替换文件:
在此示例中,我们将用替换文档替换文档。在这里,多个文档与过滤器匹配,即名称:“ Sonu”,因此replaceOne()
方法将替换这些文档中与给定条件匹配的第一个文档,如下图所示–
更换前:
更换后: