MongoDB find() method introduction

Introduction to find() method in MongoDB :

The find() method in MongoDB is used to search documents in a collection. You can get all the documents in a collection using this method based on the condition that you are providing. It takes two optional parameters :

  1. query

  2. projection

query is the filter to use and projection is the fields to return that will match the query parameter.

Both parameters are optional. If you omit the first parameter or pass one empty document {}, it will return all documents in that collection.

If you omit the second parameter, it will return all fields in the documents.

For projection, you need to pass one document like the below form :

{field : value, .....}

Here, field is the field in the document and value can be any one of the following :

  1. 1 or true to include. 0 or false to exclude.

  2. Projection Operators expression.

Get all collections using find() :

Let’s consider one collection with the below documents :

[{
	"name" : "Alex",
	"age" : 20
},
{
	"name" : "Bob",
	"age" : 21
},
{
	"name" : "Liza",
	"age" : 20
}]

If the name of the collection is students, you can use the below method to get all documents in that collection :

db.students.find()

It will print one output like below :

{ "_id" : ObjectId("5de53fe6ff1b284060972354"), "name" : "Alex", "age" : 20 }
{ "_id" : ObjectId("5de53fe6ff1b284060972355"), "name" : "Bob", "age" : 21 }
{ "_id" : ObjectId("5de53fe6ff1b284060972356"), "name" : "Liza", "age" : 20 }

If you don’t pass any parameter to the find() method, it returns all documents.

Get specific fields :

Suppose, we need only the name of all the documents. We can use the projection parameter like below for that :

db.students.find({}, {age: 0, _id: 0})

The return value will look like as below :

{ "name" : "Alex" }
{ "name" : "Bob" }
{ "name" : "Liza" }

Query with equals to :

Let’s get the document with name equal to Bob. We need to use only the query parameter for that :

db.students.find({name : "Bob"})

Output :

{ "_id" : ObjectId("5de53fe6ff1b284060972355"), "name" : "Bob", "age" : 21 }

Example to use comparison query operator :

We can use any of the comparison query operator to perform the comparison on a document field. For example, we can use the below method to get all students with age below 21 :

db.students.find({age: {$lt : 21}})

It will return :

{ "_id" : ObjectId("5de53fe6ff1b284060972354"), "name" : "Alex", "age" : 20 }
{ "_id" : ObjectId("5de53fe6ff1b284060972356"), "name" : "Liza", "age" : 20 }

MongoDB find example

Use multiple query conditions :

You can use multiple query conditions by separating them with comma. For the below documents :

[{
	"name" : "Alex",
	"age" : 20,
	"registered" : true
},
{
	"name" : "Bob",
	"age" : 21,
	"registered" : true
},
{
	"name" : "Liza",
	"age" : 20,
	"registered" : false
}]

If you want only the documents with age less than 21 and registered equals to true, you can do something like below :

db.students.find({age : { $lt : 21 } , registered : true })

It will return only the document with name Alex :

{ "_id" : ObjectId("5de5462fff1b284060972357"), "name" : "Alex", "age" : 20, "registered" : true }

MongoDB multiple query conditions

Conclusion :

In this post, we have learned the basic find() operations in MongoDB. find() is a powerful method and it is highly customizable. You can follow the official manual to learn more about it.