您可以使用DeleteOne()方法删除集合中的文档。
例子
提示
参阅“使用示例”,了解如何运行此示例。
以下示例匹配 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 operationif err != nil { panic(err)}预期结果
运行完整示例后,它将删除 movies 集合中的以下文档:
// result truncated{ "_id": ObjectId("..."), ..., "title": "Twilight", ... }有关如何查找文档的示例,请参阅查找文档。
更多信息
如要了解有关删除文档的更多信息,请参阅删除文档。