MongoDB find() 方法
介绍
在mongoDB中,find() 方法用于从表中获取特定数据。换句话说,它用于在表中选择数据。它也用于返回选择的数据的所有事件。find() 方法由两个参数组成,可以用它们来找到特定的记录。
语法
db.collection_name.find(query, projection)
- query: 这是一个可选参数,用于定义选择条件。简单来说,它定义了一个查询,即在一个集合中你想要寻找什么。
- projection: 这是一个可选参数,用于定义如果查询条件满足的话要返回什么。简单来说,它是一种决策,根据条件来做出决策。
示例
在下面的示例中,我们正在处理:
Database: JavaTpoint
Collection: student
Document: Five documents that contain the details of the students
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"name" : "Mick",
"Course" : "btech",
"batch_year" : 2018,
"language" : ["c++", "java", "python"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
"name" : "Oliver",
"Course" : "BA",
"batch_year" : 2017,
"language" : ["c", "PHP"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
}
示例1:查找学生集合中的所有文档。
当我们需要所有记录时,在查询中不使用任何参数。
db.student.find()
输出:
示例2:查找特定文档
在这个示例中,我们仅检索那些学生课程为btech的学生的文档。
db.student.find({Course : btech})
输出:
>db.student.find({Course : btech})
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"name" : "Mick",
"Course" : "btech",
"batch_year" : 2018,
"language" : ["c++", "java", "python"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
}
示例3:查找嵌入文档
在这个示例中,我们只获取与给定数组中的值匹配的学生文档。
db.student.find({score:{HTML, CSS, PHP}})
输出:
>db.student.find({score:{HTML, CSS, PHP}})
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
}
示例4:显示指定字段的文档
在这个示例中,我们通过投影来获取只有学生姓名字段的文档。
db.student.find({},{name:1, _id:0})
输出:
>db.student.find({},{name:1, _id:0})
{ "name" : "Mick" }
{ "name" : "Zoya" }
{ "name" : "Jonny" }
{ "name" : "Oliver" }
{ "name" : "Mia" }
示例5:使用limit()方法仅显示两个文档
db.student.find().limit(3)
输出:
>db.student.find().limit(3)
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"name" : "Mick",
"Course" : "btech",
"batch_year" : 2018,
"language" : ["c++", "java", "python"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
}
示例6:根据条件查找特定文档
在本示例中,我们只检索那些批次年份大于2018年的学生文档。
db.student.find({batch_year : {$gt : 2018}})
输出:
>db.student.find({batch_year : {$gt : 2018}})
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
}