MongoDB provides the find() method to select the Documents from a Collection. The find() method is similar to the Select operation of SQL. For example, if we want to see all records from the "inventory" collection then we will write db.inventory.find( {} ) in MongoDB, and in SQL we can write SELECT * FROM inventory.

Now let us see the find() method in detail.


db.collection.find()

The find() method is used to return the result set of a collection. It uses the cursor to display the first 20 results and then if you press enter then again it will iterate and display the records.

Syntax:

The following is the syntax of find() method.

db.collection_name.find(query, projection)


Let us see an example to create a collection with the name "inventory_records" in the "mongodb_test" database that we have already created in the database section and insert the below Documents in the collection. Once the Documents are inserted then we will use the find() method to project the Documents.

Example :

cloudduggu@ubuntu:~$ mongo
use mongodb_test
db.inventory_records.insertMany([ { "item_name":"canvas", "item_qty":110, "item_size":{ "height":29, "weight":36.5, "uom":"cm" }, "status":"A" }, { "item_name":"journal", "item_qty":35, "item_size":{ "height":15, "weight":22, "uom":"cm" }, "status":"A" }, { "item_name":"mat", "item_qty":95, "item_size":{ "height":23.9, "weight":32.5, "uom":"cm" }, "status":"A" }, { "item_name":"mousepad", "item_qty":45, "item_size":{ "height":16, "weight":28.85, "uom":"cm" }, "status":"P" }, { "item_name":"notebook", "item_qty":60, "item_size":{ "height":7.5, "weight":10, "uom":"in" }, "status":"P" }, { "item_name":"paper", "item_qty":200, "item_size":{ "height":9.5, "weight":12, "uom":"in" }, "status":"D" }, { "item_name":"planner", "item_qty":75, "item_size":{ "height":25.85, "weight":40, "uom":"cm" }, "status":"D" }, { "item_name":"postcard", "item_qty":25, "item_size":{ "height":15, "weight":17.25, "uom":"cm" }, "status":"A" }, { "item_name":"sketchbook", "item_qty":90, "item_size":{ "height":13, "weight":23, "uom":"cm" }, "status":"A" }, { "item_name":"sketch_pad", "item_qty":85, "item_size":{ "height":25.85, "weight":32.5, "uom":"cm" }, "status":"A" } ]);

db.inventory_records.find( {  } )


This operation can be written as below in SQL Language.

SELECT * from inventory_records;


Output:

In the below figure we can see the Documents are inserted in the "inventory_records" Collection using the insertmany() method.

Mongodb insert many command

Mongodb insert many command

In the below figure, we can see that the find() method has projected all Documents from the "inventory_records" Collection.

Mongodb find command


db.collection.findOne()

The findone() method is used to return only one Document.

Syntax:

The following is the syntax of pretty() method.

db.collection_name.findOne()


In the below example we will use the findone() method to find out the matching records where item_name = "canvas".

Example:

db.inventory_records.findOne( { item_name: "canvas" } )


We can write the same operation in SQL language as mentioned below.

SELECT * FROM inventory_records WHERE item_name= "canvas"


Output:

Mongodb findone command


db.collection.find().pretty()

The pretty() method is used to show the result in an easy-to-read format.

Syntax:

The following is the syntax of pretty() method.

db.collection_name.find(<query>).pretty()


Let us see the below example to show the "inventory_records" Collection Documents in a readable format using the pretty() method.

Example:

db.inventory_records.find().pretty()


Output:

We can see from the below output that the Documents are returned in a formatted way.

Mongodb pretty command


AND Condition in MongoDB

MongoDB supports the and condition to compare two Documents and return the result when both Documents conditions are matching. To use and condition we can use the $and operator.

Syntax:

The syntax of AND condition is as below.

db.collection_name.find({ $and: [ {key_name_1:value_detail_1}, {key_name_2:value_detail_2} ] })


Example:

Let us see the below example in which we are fetching all Documents where status equals "A" and item_qtyqty is less than 30.

db.inventory_records.find({$and:[ {"status":"A"}, {item_qty: { $lt: 30}} ]})


In SQL language the same operation is written as below.

SELECT * FROM inventory_records WHERE status = "A" AND item_qty < 30


Output:

From the below output we can see only the below Document is qualifying the and condition and the result is returned.

Mongodb and condition


OR Condition in MongoDB

In MongoDB or condition takes two Documents and check if either of the condition is matching then it returns the matching Documents. We can use the MongoDB < or condition using the $or operator.

Syntax:

The syntax of OR condition is as below.

db.collection_name.find({ $or: [ {keyname_1:valuedetail_1},  {keyname_2:valuedetail_2} ] })


Example:

Let us see the below example in which All Documents are returned where the status equals to "D" or item_qty is less than 30.

db.inventory_records.find({$or:[ {"status":"D"}, {item_qty: { $lt: 30}} ]})


We can write the same operation in SQL as mentioned below.

SELECT * FROM inventory_records WHERE status = "D" OR item_qty < 30


Output:

We can see in the below output that all matching Documents are returned.

Mongodb or condition


AND & OR Condition Together in MongoDB

Let us see the example of and & or condition together in which we are fetching all Documents where the status=p and item_qty are less than 80 or the item_name starts with the P character.

Example:

db.inventory_records.find( {
     status: "P",
     $or: [ { item_qty: { $lt: 80 } }, { item_name: /^p/ } ]
} )


In the SQL the operation is written as below.

SELECT * FROM inventory WHERE status = "P" AND ( item_qty < 80 OR item_name LIKE "p%")


Output:

The Matching Documents are returned in the below output.

Mongodb and & or condition


NOT Condition in MongoDB

The MongoDB NOT condition applies the logical NOT operation and returns the Documents which are not matching.

Syntax:

The syntax of NOT condition is as below.

db.collection_name.find({ $not: [ {key_1: value_1},  {key_2: value_2} ] })


Example:

In the below example we will use the NOT operator to fetch the Documents where the item_qty is less than or equal to 60.

db.inventory_records.find( { item_qty: { $not: { $gt: 60 } } } )


The SQL version of the same operation is as below.

SELECT * FROM inventory WHERE item_qty !> 60;


Output:

Mongodb not command condition