1. 概述
在本教程中,我们将着眼于执行搜索操作以在MongoDB中检索文档。MongoDB 提供了一个find操作符来从集合中查询文档。find运算符的主要目的是根据查询条件从集合中选择文档,并将光标返回到所选文档。
在本教程中,我们将首先查看 MongoDB Shell 查询中的find运算符,然后使用 Java 驱动程序代码。
2. 数据库初始化
在我们继续执行查找操作之前,我们首先需要设置一个数据库baeldung和一个样本收集员工:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | db.employee.insertMany([ { "employeeId" : "EMP1" , "name" : "Sam" , "age" :23, "type" : "Full Time" , "department" : "Engineering" }, { "employeeId" : "EMP2" , "name" : "Tony" , "age" :31, "type" : "Full Time" , "department" : "Admin" }, { "employeeId" : "EMP3" , "name" : "Lisa" , "age" :42, "type" : "Part Time" , "department" : "Engineering" }]); |
成功插入后,上述查询将返回类似于下图所示的 JSON 结果:
1 2 3 4 5 6 7 8 | { "acknowledged" : true , "insertedIds" : [ ObjectId( "62a88223ff0a77909323a7fa" ), ObjectId( "62a88223ff0a77909323a7fb" ), ObjectId( "62a88223ff0a77909323a7fc" ) ] } |
此时,我们已将一些文档插入到我们的集合中以执行各种类型的查找操作。
3. 使用 MongoDB Shell
要从 MongoDB 集合中查询文档,我们使用 db.collection.find(query, projection)
方法。该方法接受两个可选参数— 查询(query) 和 投影(projection) —作为 MongoDB BSON文档。
查询参数接受带有查询运算符的选择过滤器。要从 MongoDB 集合中检索所有文档,我们可以省略此参数或传递一个空白文档。
接下来,投影参数用于指定要从匹配文档返回的字段。要返回匹配文档中的所有字段,我们可以省略此参数。
此外,让我们从返回所有集合文档的基本查找查询开始:
1 | db.employee.find({<! --{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->}); |
上面的查询将返回员工集合中的所有文档:
1 2 3 | { "_id" : ObjectId( "62a88223ff0a77909323a7fa" ), "employeeId" : "1" , "name" : "Sam" , "age" : 23, "type" : "Full Time" , "department" : "Engineering" } { "_id" : ObjectId( "62a88223ff0a77909323a7fb" ), "employeeId" : "2" , "name" : "Tony" , "age" : 31, "type" : "Full Time" , "department" : "Admin" } { "_id" : ObjectId( "62a88223ff0a77909323a7fc" ), "employeeId" : "3" , "name" : "Ray" , "age" : 42, "type" : "Part Time" , "department" : "Engineering" } |
*接下来,让我们编写一个查询来返回属于“Engineering” *部门的所有员工:
1 2 3 4 | db.employee.find( { "department" : "Engineering" }); |
上述查询返回部门等于 “Engineering”的所有员工收款单据:
1 2 | { "_id" : ObjectId( "62a88223ff0a77909323a7fa" ), "employeeId" : "1" , "name" : "Sam" , "age" : 23, "type" : "Full Time" , "department" : "Engineering" } { "_id" : ObjectId( "62a88223ff0a77909323a7fc" ), "employeeId" : "3" , "name" : "Ray" , "age" : 42, "type" : "Part Time" , "department" : "Engineering" } |
最后,让我们编写一个查询来获取属于“Engineering”部门的所有员工的 姓名 和 年龄:
1 2 3 4 5 6 7 8 | db.employee.find( { "department" : "Engineering" }, { "name" :1, "age" :1 }); |
上述查询只返回符合查询条件的文档的名称和年龄字段:
1 2 | { "_id" : ObjectId( "62a88223ff0a77909323a7fa" ), "name" : "Sam" , "age" : 23 } { "_id" : ObjectId( "62a88223ff0a77909323a7fc" ), "name" : "Ray" , "age" : 42 } |
请注意,除非明确排除,否则所有文档中默认返回_id
字段。
此外,重要的是要注意 find 运算符将光标返回到与查询过滤器匹配的文档。MongoDB Shell 自动迭代光标以显示多达 20 个文档。
此外,MongoDB Shell 提供了一个*findOne()*方法,该方法只返回一个满足上述查询条件的文档。如果多个文档匹配,则将按照磁盘上文档的自然顺序返回第一个文档:
1 | db.employee.findOne(); |
与*find()*不同,上面的查询将只返回一个文档而不是游标:
1 2 3 4 5 6 7 8 | { "_id" : ObjectId( "62a99e22a849e1472c440bbf" ), "employeeId" : "EMP1" , "name" : "Sam" , "age" : 23, "type" : "Full Time" , "department" : "Engineering" } |
4. 使用 Java 驱动程序
到目前为止,我们已经了解了如何使用 MongoDB Shell 来执行查找操作。接下来,让我们使用 MongoDB Java 驱动程序实现相同的功能。在开始之前,让我们先创建一个到员工集合的MongoClient连接:
1 2 3 | MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); MongoDatabase database = mongoClient.getDatabase( "baeldung" ); MongoCollection collection = database.getCollection( "employee" ); |
在这里,我们创建了到运行在默认端口 27017 上的 MongoDB 服务器的连接。接下来,我们从连接创建的*MongoDatabase实例中获取MongoCollection的实例。
首先,要执行*查找操作,我们在MongoCollection的实例上调用 find()
方法。让我们检查代码以从集合中检索所有文档:
1 2 3 4 5 | FindIterable documents = collection.find(); MongoCursor cursor = documents.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } |
请注意,find()方法返回FindIterable
的一个实例。然后我们通过调用 FindIterable 的iterator()方法获得MongoCursor的一个实例。最后,我们遍历光标以检索每个文档。
接下来,让我们添加查询运算符来过滤从查找操作返回的文档:
1 2 3 4 5 6 7 | Bson filter = Filters.eq( "department" , "Engineering" ); FindIterable documents = collection.find(filter); MongoCursor cursor = documents.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } |
在这里,我们将Bson过滤器作为参数传递给find()方法。我们可以使用查询运算符的任意组合作为find()方法的过滤器。上面的代码片段将返回department 等于“Engineering”的所有文档。
此外,让我们编写一个片段,它只返回匹配选择条件的文档中的姓名和年龄字段:
1 2 3 4 5 6 7 8 9 | Bson filter = Filters.eq( "department" , "Engineering" ); Bson projection = Projections.fields(Projections.include( "name" , "age" )); FindIterable documents = collection.find(filter) .projection(projection); MongoCursor cursor = documents.iterator(); while (cursor.hasNext()) { System.out.println(cursor.next()); } |
在这里,我们调用FindIterable实例的projection()方法。我们将Bson过滤器作为参数传递给*projection()*方法。我们可以使用投影操作在最终结果中包含或排除任何字段。
最后,我们可以使用FindIterable实例上的first()方法检索结果的第一个文档。这将返回单个文档而不是MongoCursor实例:
1 2 | FindIterable documents = collection.find(); Document document = documents.first(); |
5. 结论
在本文中,我们学习了使用各种方法在 MongoDB 中执行查找操作。我们执行find以使用查询运算符检索与选择标准匹配的特定文档。此外,我们还学习了执行投影以确定匹配文档中返回的字段。
首先,我们研究了 MongoDB Shell 查询中find*操作的用例,然后讨论了相应的 Java 驱动程序代码。
到此这篇关于MongoDB中查询(find操作符)的文章就介绍到这了,更多相关MongoDB查询find操作符内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!