在MongoDB中,可以使用db.collection.deleteMany()
方法从集合中删除现有文档。此方法根据过滤器从集合中删除多个文档。
deleteMany()
是一个mongo shell方法,可以删除多个文档。此方法可用于多文档交易中。如果在上限集合中使用此方法,则它将引发异常。
Syntax:
Parameters:
fileter: First parameter of this method. It specifies the selection criteria for the delete using query operators. The type of this parameter is document. If it contains empty document, i.e, {}, then this method will delete all the documents from the collection.
Optional Parameters:
- 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.
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) and deletedCount that represents the total number of deleted documents.
例子:
在以下示例中,我们正在使用:
db.collection.deleteMany(
,
{
writeConcern: ,
collation:
}
)
删除与过滤器匹配的文档:
在此示例中,我们将从与过滤器匹配的贡献者集合中删除多个文档,即语言:“ C#”。换句话说,我们正在从数据库中删除那些使用C#语言的贡献者。
删除所有文件:
db.collection.deleteMany()
方法中传递空文档来从贡献者集合中删除所有文档。
Database: GeeksforGeeks
Collection: contributor
Document: four documents that contain the details of the contributors in the form of field-value pairs.