MongoDB find() MethodIntroductionIn 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:
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: Example 2: Find Specific DocumentsIn 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 documentIn 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 fieldsIn 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() methodOutput: >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 criteriaIn 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"], } Next TopicMongoDB $group operator |