Docs 菜单

Delete Documents

在本指南中,您可以了解如何使用 MongoDB Java 驱动程序删除文档。

您可以通过将查询筛选器传递给deleteOne()deleteMany()findOneAndDelete()方法来删除文档。

deleteOne()方法删除单个文档。 如果查询筛选器匹配多个文档,该方法将删除集合中第一次出现的匹配项。

deleteMany()方法删除与查询筛选器匹配的所有文档。

findOneAndDelete()方法自动查找并删除collection中第一次出现的匹配项。

要指定排序规则或提示索引,请将DeleteOptions用作deleteOne()deleteMany()方法的第二个参数。

要在返回的文档上指定排序规则、提示索引、指定排序顺序或指定投影,请将FindOneAndDeleteOptions作为findOneAndDelete()方法的第二个参数。

以下示例涉及一家销售八种不同颜色油漆的油漆店。 paint_inventory该商店进行了年度在线销售,导致其collection中出现了以下文档:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 3, "color": "blue", "qty": 0 }
{ "_id": 4, "color": "white", "qty": 0 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 6, "color": "pink", "qty": 0 }
{ "_id": 7, "color": "green", "qty": 0 }
{ "_id": 8, "color": "black", "qty": 8 }

油漆店网站显示paint_inventory集合中的所有文档。 为了减少客户混淆,商店希望删除缺货的颜色。

要删除缺货颜色,请查询paint_inventory qty0为 的collection,然后将查询传递给deleteMany() 方法:

Bson filter = Filters.eq("qty", 0);
collection.deleteMany(filter);

下面显示了在paint_inventory collection中剩余的文档:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 8, "color": "black", "qty": 8 }

商店正在捐赠剩余数量的黄色油漆。 这意味着黄色的qty现在是0 ,我们需要从集合中删除黄色。

要删除黄色,请查询 paint_inventory 集合,其中 color"yellow",并将查询传递给 deleteOne() 方法:

Bson filter = Filters.eq("color", "yellow");
collection.deleteOne(filter);

下面显示了在paint_inventory collection中剩余的文档:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 8, "color": "black", "qty": 8 }

该商店希望对剩余数量的紫色颜料进行抽奖,并从paint_inventory collection 中删除紫色。

要选择颜色,请查询color"purple"paint_inventory集合,并将查询传递给findOneAndDelete()方法:

Bson filter = Filters.eq("color", "purple");
System.out.println(collection.findOneAndDelete(filter).toJson());

与其他删除方法不同, findOneAndDelete()返回已删除的文档:

{ "_id": 2, "color": "purple", "qty": 8 }

注意

如果查询筛选器没有匹配项,则不会删除任何文档,并且该方法会返回null

下面显示了在paint_inventory collection中剩余的文档:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 8, "color": "black", "qty": 8 }

注意

Example Setup

This example connects to an instance of MongoDB by using a connection URI. To learn more about connecting to your MongoDB instance, see the 创建 MongoClient guide. This example also uses the movies collection in the sample_mflix database included in the Atlas sample datasets. You can load them into your database on the free tier of MongoDB Atlas by following the Get Started with Atlas Guide.

The following code is a complete, standalone file that performs a delete one operation and a delete many operation:

// Deletes documents from a collection by using the Java driver
package org.example;
import static com.mongodb.client.model.Filters.eq;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Filters.lt;
public class Delete {
public static void main(String[] args) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");
Bson deleteOneQuery = eq("title", "The Garbage Pail Kids Movie");
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
DeleteResult result = collection.deleteOne(deleteOneQuery);
System.out.println("Deleted document count - delete one: " + result.getDeletedCount());
Bson deleteManyQuery = lt("imdb.rating", 1.9);
// Deletes all documents that have an "imdb.rating" value less than 1.9
result = collection.deleteMany(deleteManyQuery);
// Prints the number of deleted documents
System.out.println("Deleted document count - delete many: " + result.getDeletedCount());
}
}
}
Deleted document count - query for one: 1
Deleted document count - unlimited query: 4

The queries in these examples use the eq() and lt() filters to query documents. For more information about filters, see the Filters Class API documentation.

For more information about the methods and classes used to delete documents, see the following API documentation: