Docs 菜单
Docs 主页
/ / /
Java (Sync) 驱动程序
/

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 }

注意

设置示例

此示例使用连接 URI 连接到MongoDB实例。要学习;了解有关连接到MongoDB实例的更多信息,请参阅创建 MongoClient指南。此示例还使用 Atlas示例数据集包含的 sample_mflix数据库中的 movies集合。您可以按照Atlas入门指南,将它们加载到MongoDB Atlas免费套餐上的数据库中。

以下代码是完整的独立运行文件,用于执行删除一个操作和删除多个操作:

// 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

这些示例中的查询使用 eq()lt() 筛选器来查询文档。有关筛选器的更多信息,请参阅筛选器类 API文档。

有关用于删除文档的方法和类的更多信息,请参阅以下API文档:

后退

替换文档

在此页面上