Javatpoint Logo
Javatpoint Logo

MongoDB find() Method

Introduction

In mongoDB, the find() method is used to fetch a particular data from the table. In other words, it is used to select data in a table. It is also used to return all events to the selected data. The find() method consists of two parameters by which we can find a particular record.

Syntax:

  1. query: This is an optional parameter that defines the selection criteria. In simple words, it defines a query as what you want to find in a collection.
  2. projection: This is an optional parameter that defines what to return if the query criteria are met successfully. In simple words, it is a type of decision-making that decides on criteria.
MongoDB find() Method

Examples:

In the following examples, we are working with:

{
           "_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"],
}

Example 1: Find all documents in the collection of the student.

When we need all the records, we don't use any parameter in the query.

Output:

MongoDB find() Method

Example 2: Find Specific Documents

In this example, we are only retrieving those student's documents whose student course is btech.

Output:

>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"],
}

Example 3: Find the embedded document

In this example, we are only retrieving those students' documents that match the values at the given array.

Output:

>db.student.find({score:{HTML, CSS, PHP}})
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "Mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
}

Example 4: Display documents with specified fields

In this example, we are retrieving only student name fields with the help of projection.

Output:

>db.student.find({},{name:1, _id:0})
{ "name" : "Mick" }
{ "name" : "Zoya" }
{ "name" : "Jonny" }
{ "name" : "Oliver" }
{ "name" : "Mia" }

Example 5: Display only two documents using the limit() method

Output:

>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"],
}

Example 6: Find the specific documents with conditional criteria

In this example, we are only retrieving those students' documents whose batch_year is greater than 2018.

Output:

>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"],
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA