MongoDb Find Query

The MongoDb provides find() command to fetch data from the collection. It returns documents that match the query criteria.

Syntax of Find Document

 
db.collection.find(query, projection)
 

query - It is of document type and an optional parameter. It contains selection filters using query parameters.

projection - It is of document type and an optional parameter. It specifies the fields to return in a document that match the query parameter.

The find categories in two categories -

  • findOne()
  • find()

MongoDb findOne()

It returns only the first document of a collection that fulfill the specified criteria.

Example

db.students.findOne({"name" : "Rana Soi"})

Here, it returns only one document of the name 'Rana Soi'.

mongodb find one document

MongoDb find()

The MongoDb find() method is used to return the document or documents based on the specified criteria. To return all documents, use find() method without any parameter or filter criteria.

db.students.find()




MongoDb find() by id

The following operation returns only those documents whose _id is "5d46bea61c6d5fc35f3fc4a1".

> db.students.find({_id: ObjectId("5d46bea61c6d5fc35f3fc4a1")});
{ "_id" : ObjectId("5d46bea61c6d5fc35f3fc4a1"), "name" : "Andy Joya", "age" : 10, "class" : "5A" }

MongoDb find() specific fields

To return filtered documents, pass the filtered criteria to find() method. It is similar to the WHERE statement of MySQL. Suppose we want to get all students of age 10, for this run the following command-

db.students.find({"age": 10})
mongodb find one document

MongoDb find() pretty

To get data more readable, use pretty() method. This method formats the returned data.

db.students.find({"age": 11}).pretty()
mongodb find one document

MongoDb explain()

It provides information on the query plan for the find() method.

Syntax of explain()

 
db.collection.find().explain()
 

Example

> db.students.find({class: "5A"}).explain()
{
"queryPlanner" : {
	"plannerVersion" : 1,
	"namespace" : "school.students",
	"indexFilterSet" : false,
	"parsedQuery" : {
		"class" : {
			"$eq" : "5A"
		}
	},
	"winningPlan" : {
		"stage" : "COLLSCAN",
		"filter" : {
			"class" : {
				"$eq" : "5A"
			}
		},
		"direction" : "forward"
	},
	"rejectedPlans" : [ ]
},
"serverInfo" : {
	"host" : "DESKTOP-NAME",
	"port" : 27017,
	"version" : "4.0.10",
	"gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
},
"ok" : 1
}




MongoDb find() with Projection

The projection in MongoDb find() is used for selecting only necessary data rather than selecting the whole data.

Syntax of find() with projection

 
db.collection_name.find({},{KEY:1});
 

Here, 1 is used to show field while 0 is used to hide the field.

Example of projection

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

The above command returns all the documents without age field.

db.students.find({},{age:1})

The above command returns all the documents with only age field. Similarly, if we want to hide the _id field, we can write operation as-

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

MongoDb find() with query and projection

This is used to return specific fields with applied conditions. Suppose we want to return 'name' and 'age' of class '5A'.

> db.students.find({class: "5A"},{name: 1, age:1})
{ "_id" : ObjectId("5d46be021c6d5fc35f3fc49f"), "name" : "Zorj Roi", "age" : 10 }
{ "_id" : ObjectId("5d46bea61c6d5fc35f3fc4a1"), "name" : "Andy Joya", "age" : 10 }

MongoDb distinct() method

The distinct() method is used to find the distinct values for the given field across a collection.

Syntax of distinct()

 
db.collection_name.distinct(field, query)
 

field - fields on which returns distinct values.
query - contains selection filters using query parameters.

Example of distinct

> db.students.distinct("class");
[ 8, "5A", "6B", "5B", "6A" ]

The above operation returns all the distinct classes.