Open In App

MongoDB – Find() Method

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The find () method in MongoDB selects documents in a collection that matches the specified conditions and returns a cursor to the selected documents.

A cursor means a pointer that points to a document, when we use the find() method it returns a pointer on the selected documents and returns one by one. If we want to return a pointer on all documents then use empty() parameter that returns all documents one by one. It takes only some optional parameters. The first optional parameter is the selection criteria on which we want to return a cursor. To return all documents in a collection use empty document({}).

Using this method you can also replace embedded documents. You can also use this method in multi-document transactions. If you use this method in the Mongo shell, then the shell will automatically iterate the cursor to display up to 20 documents in the collection, if you want to continue then type it or you can manually iterate the result of the find() method by assigning the returned cursor to a variable with the var keyword. You can also modify the behavior of this method using cursor methods.

Syntax

db.Collection_name.find(selection_criteria, projection,options)

Parameters:

  • selection_criteria: It specifies selection criteria. To return all documents in a collection use empty document({}). The type of this parameter is a document.
  • projection: It specifies the fields to return in the documents that match the selection criteria. To return all fields in the matching documents, remove this parameter. It is of the document type.
  • options: It specifies some additional options for the selection_criteria parameter. It modifies the behavior of selection_criteria and also affects the results that will be returned.

This document takes:

{ field1: <value1>, field2: <value2> ... }

Here if the value of the field is 1/true then it specifies the inclusion of the field, or if the value of the field is 0/false then it specifies the exclusion of the field.

Return:

It returns a cursor to the documents that match the selection criteria. When the find() method returns documents, the method is actually returning a cursor to the documents.

MongoDB find Method Examples

In the following example, we are working with:

Database: gfg

Collections: student

Document: Three documents contains the details of the students

Example 1:

Find all the document that matches the given filter query(i.e., age:18):

db.student.find({age:18})

mongodb find method example output

Example 2:

Find all the documents present in the collection:

db.student.find()

find all documents in collection example output

Example 3:

Find the embedded document that matches the given filter query:

db.student.find({score:{math: 230, science: 234}})

find embedded document in mongodb example output

Conclusion

The MongoDB find method is used to find specific documents in a collection. It can also be used to return all documents in a collection by passing no parameters, or by passing empty document in the function.

This tutorial covered the find method in MongoDB with examples. The examples are explained for multiple use cases of the method.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads