1. 概述
在本教程中,我们将着眼于执行搜索操作以在mongodb中检索文档。mongodb 提供了一个find操作符来从集合中查询文档。find运算符的主要目的是根据查询条件从集合中选择文档,并将光标返回到所选文档。
在本教程中,我们将首先查看 mongodb shell 查询中的find运算符,然后使用 java 驱动程序代码。
2. 数据库初始化
在我们继续执行查找操作之前,我们首先需要设置一个数据库baeldung和一个样本收集员工:
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 结果:
{ "acknowledged" : true, "insertedids" : [ objectid("62a88223ff0a77909323a7fa"), objectid("62a88223ff0a77909323a7fb"), objectid("62a88223ff0a77909323a7fc") ] }
此时,我们已将一些文档插入到我们的集合中以执行各种类型的查找操作。
3. 使用 mongodb shell
要从 mongodb 集合中查询文档,我们使用 db.collection.find(query, projection)
方法。该方法接受两个可选参数— 查询(query) 和 投影(projection) —作为 mongodb bson文档。
查询参数接受带有查询运算符的选择过滤器。要从 mongodb 集合中检索所有文档,我们可以省略此参数或传递一个空白文档。
接下来,投影参数用于指定要从匹配文档返回的字段。要返回匹配文档中的所有字段,我们可以省略此参数。
此外,让我们从返回所有集合文档的基本查找查询开始:
db.employee.find({<!--{cke_protected}{c}%3c!%2d%2d%20%2d%2d%3e-->});
上面的查询将返回员工集合中的所有文档:
{ "_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” *部门的所有员工:
db.employee.find( { "department":"engineering" });
上述查询返回部门等于 “engineering”的所有员工收款单据:
{ "_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”部门的所有员工的 姓名 和 年龄:
db.employee.find( { "department":"engineering" }, { "name":1, "age":1 });
上述查询只返回符合查询条件的文档的名称和年龄字段:
{ "_id" : objectid("62a88223ff0a77909323a7fa"), "name" : "sam", "age" : 23 } { "_id" : objectid("62a88223ff0a77909323a7fc"), "name" : "ray", "age" : 42 }
请注意,除非明确排除,否则所有文档中默认返回_id
字段。
此外,重要的是要注意 find 运算符将光标返回到与查询过滤器匹配的文档。mongodb shell 自动迭代光标以显示多达 20 个文档。
此外,mongodb shell 提供了一个*findone()*方法,该方法只返回一个满足上述查询条件的文档。如果多个文档匹配,则将按照磁盘上文档的自然顺序返回第一个文档:
db.employee.findone();
与*find()*不同,上面的查询将只返回一个文档而不是游标:
{ "_id" : objectid("62a99e22a849e1472c440bbf"), "employeeid" : "emp1", "name" : "sam", "age" : 23, "type" : "full time", "department" : "engineering" }
4. 使用 java 驱动程序
到目前为止,我们已经了解了如何使用 mongodb shell 来执行查找操作。接下来,让我们使用 mongodb java 驱动程序实现相同的功能。在开始之前,让我们先创建一个到员工集合的mongoclient连接:
mongoclient mongoclient = new mongoclient("localhost", 27017); mongodatabase database = mongoclient.getdatabase("baeldung"); mongocollection<document> collection = database.getcollection("employee");
在这里,我们创建了到运行在默认端口 27017 上的 mongodb 服务器的连接。接下来,我们从连接创建的*mongodatabase实例中获取mongocollection的实例。
首先,要执行*查找操作,我们在mongocollection的实例上调用 find()
方法。让我们检查代码以从集合中检索所有文档:
finditerable<document> documents = collection.find(); mongocursor<document> cursor = documents.iterator(); while (cursor.hasnext()) { system.out.println(cursor.next()); }
请注意,find()方法返回finditerable<document>
的一个实例。然后我们通过调用 finditerable 的iterator()方法获得mongocursor的一个实例。最后,我们遍历光标以检索每个文档。
接下来,让我们添加查询运算符来过滤从查找操作返回的文档:
bson filter = filters.eq("department", "engineering"); finditerable<document> documents = collection.find(filter); mongocursor<document> cursor = documents.iterator(); while (cursor.hasnext()) { system.out.println(cursor.next()); }
在这里,我们将bson过滤器作为参数传递给find()方法。我们可以使用查询运算符的任意组合作为find()方法的过滤器。上面的代码片段将返回department 等于“engineering”的所有文档。
此外,让我们编写一个片段,它只返回匹配选择条件的文档中的姓名和年龄字段:
bson filter = filters.eq("department", "engineering"); bson projection = projections.fields(projections.include("name", "age")); finditerable<document> documents = collection.find(filter) .projection(projection); mongocursor<document> cursor = documents.iterator(); while (cursor.hasnext()) { system.out.println(cursor.next()); }
在这里,我们调用finditerable实例的projection()方法。我们将bson过滤器作为参数传递给*projection()*方法。我们可以使用投影操作在最终结果中包含或排除任何字段。
最后,我们可以使用finditerable实例上的first()方法检索结果的第一个文档。这将返回单个文档而不是mongocursor实例:
finditerable<document> documents = collection.find(); document document = documents.first();
5. 结论
在本文中,我们学习了使用各种方法在 mongodb 中执行查找操作。我们执行find以使用查询运算符检索与选择标准匹配的特定文档。此外,我们还学习了执行投影以确定匹配文档中返回的字段。
首先,我们研究了 mongodb shell 查询中find*操作的用例,然后讨论了相应的 java 驱动程序代码。
到此这篇关于mongodb中查询(find操作符)的文章就介绍到这了,更多相关mongodb查询find操作符内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论