MongoDB

How to use find method in MongoDB

MongoDB is a database that stores data in the form of documents and then provides its own query language named MongoDB Query Language (MQL) for manipulating the data. Several methods can be used to fetch documents, they include, find(), findAndModify(), findOne(), findOneAndDelete() etc. Among these, the find() method is used the most as it can generally be utilized for every retrieval. The find() method is the core part of any Mongo-based query that is directed to retrieve documents. The find method helps to retrieve documents based on some conditions and parameters that are defined in it.

In this article, we will provide an insight into the basics of the find() method and will guide you to use it in Mongo queries.

How find() method works in MongoDB

The syntax that is used for the find() method is written below:

find(query, projection)

Or:

find()

There are two syntaxes shown above. The first find() method syntax passes two parameters that help to retrieve documents based on some conditions whereas the second syntax fetches all the documents present in the collection.

query: This parameter allows you to fetch specific documents in a collection, by defining its functionality in a query.

projection: When the query criteria are met, the projection parameter decides what to return after query execution.

Note: Both parameters are optional and are used if needed, otherwise find() method can be executed alone.

How to use find() method in MongoDB

This section briefly describes the usage of find() method in MongoDB: For this, we will use following MongoDB instances:

Database: The database referred in this post is “linuxhint

Collection: And the collection associated with linuxhint database is named as “library

Example 1: Fetching all documents in collection

The basic use of the find() method is to fetch all documents in a collection: For instance, the command mentioned below will print all the documents in the “library” collection of the “linuxhint” database:

> db.library.find()

Text Description automatically generated

Example 2: Fetching specific documents in a collection

By using the find() method without passing parameters, you may get some unwanted documents as well that you do not need. To avoid such conditions, the find() method supports Mongo queries to fetch only those documents that fulfill the condition.

The command written below will print only those documents in which the “cat” field equals to “Novel“:

> db.library.find({cat: "Novels"})

Example 3: Using find() method with other operators

The find() method can be used with several operators to get the documents that fulfill the condition.

Conditional Operators: All the conditional operators supported by MongoDB can be used inside the find() method to get refined results. The command mentioned below prints the documents that have “quantity” greater than or equals to “100”:

> db.library.find({quantity: {$gte: 100}})

Text Description automatically generated

Similarly, other conditional operators like $lt, $gte, $lte can also be tried as well to meet any condition.

Logical operators: The logical operators supported by MongoDB include $and, $or, $nor, $not, and these all are used inside the find() method for logical execution of any Mongo query.

For instance, the command mentioned here will print those documents that have quantity greater than 100 or ($or) price is less than 2000. It is observed that the below-mentioned command exercises the use of logical and conditional operators in a single command.

> db.library.find({$or: [{quantity: {$gt: 100}}, {price: {$lt: 2000}}]})

A screenshot of a computer Description automatically generated with medium confidence

Array query operators : The command below will fetch documents based on execution of $size operator that belongs to “array query operator” class of MongoDB:

> db.library.find({shelves: {$size: 3}})

Conclusion

MongoDB supports an extensive list of methods that have several purposes. The find() method is one of the most used MongoDB methods to retrieve documents from a collection. In this article, we have provided a brief introduction, the working mechanism, and how the find() method is used in Mongo databases. As MongoDB has a document-based structure of storing data, it is noticed that the flexibility of the find() method has eased the querying process to process documents in MongoDB. This guide will act as a gem for novice MongoDB enthusiasts because find() method is the core part of MongoDB retrieval queries.

About the author

Adnan Shabbir