“文档” 菜单
文档首页
/ / /
Go 驱动程序
/ /

删除文档

您可以使用 DeleteOne()方法删除collection中的文档。

提示

阅读用法示例,了解如何运行此示例。

以下示例匹配 movies 集合中 title 为“Twilight”的文档,并删除第一个匹配的文档:

coll := client.Database("sample_mflix").Collection("movies")
filter := bson.D{{"title", "Twilight"}}
// Deletes the first document that has a "title" value of "Twilight"
result, err := coll.DeleteOne(context.TODO(), filter)
// Prints a message if any errors occur during the operation
if err != nil {
panic(err)
}

查看 完全可运行的示例

运行完整示例后,它将删除 movies 集合中的以下文档:

// result truncated
{ "_id": ObjectId("..."), ..., "title": "Twilight", ... }

有关如何查找文档的示例,请参阅查找文档

如要了解有关删除文档的更多信息,请参阅删除文档

DeleteOne()

← 替换文档