Java的MongoDB:检索和删除文档

时间:2024-03-19 17:36:59

Java的MongoDB:检索和删除文档 (MongoDB with Java: Retrieve and Delete Document)

In this tutorial we will learn how we can retrieve or fetch data from any document stored in a collection and how to delete any existing document stored in any collection.

在本教程中,我们将学习如何从集合中存储的任何文档中检索或获取数据,以及如何删除任何集合中存储的现有文档。

These functions are equivalent to SELECT and DELETE query in MySQL

这些功能等效于MySQL中的SELECTDELETE查询

检索文件 (Retrieve a Document)

If we want to retrieve data against field friends from document with _id xyz123, it can be done with the help of below code snippet:

如果我们想使用_id xyz123从文档中检索针对领域friends数据,则可以在以下代码段的帮助下完成:

Java的MongoDB:检索和删除文档



The output produced on console is:

在控制台上产生的输出是:

Java的MongoDB:检索和删除文档

删除文件 (Delete a Document)

Now, in our friends field, we have data as "andy", "John" and "Amit". If we want to remove "John" from the list, we can with the help of the below code.

现在,在我们的“ 朋友”字段中,我们的数据为“ andy”“ John”“ Amit” 。 如果要从列表中删除“ John” ,可以在以下代码的帮助下进行。

Java的MongoDB:检索和删除文档



The output of the query is:

查询的输出是:

Java的MongoDB:检索和删除文档



"John" is removed from the field friends.

“约翰”被从田野友人中删除。

翻译自: https://www.studytonight.com/mongodb/retrieve-delete-mongodb