find() command in MongoDB

The find() command in MongoDB is used to find the data from the collection. There are various options for executing the find() command in MongoDB. In this post, we will be using these options along with the find() command.

To execute the find() command we need to connect to our MongoDB server by using MongoShell. Instead of managing the MongoDB server ourselves, It will be easier to use a managed MongoDB called Atlas. Hence, you can visit my earlier post to connect MongoDB Atlas cluster using shell if you don’t know already.

In this tutorial, we will be using a cluster that was already created in Atlas and also the sample dataset. You can visit my earlier post create cluster in MongoDB where I’ve explained everything in detail.

I can assume that you’ve already:

  • Created a cluster in Atlas and also loaded a sample dataset
  • Connected to Atlas Cluster using shell

After connecting to the MongoDB cluster, we can see the list of databases by using the command:

show databases

It lists all the databases.

admin               0.000GB
local               3.705GB
sample_airbnb       0.051GB
sample_analytics    0.009GB
sample_geospatial   0.001GB
sample_guides       0.000GB
sample_mflix        0.047GB
sample_restaurants  0.006GB
sample_supplies     0.001GB
sample_training     0.055GB
sample_weatherdata  0.003GB

Now, we can go to our database by using the command:

use sample_training

This command lets us go inside sample_training database where we can see the multiple collections by using the command below:

show collections

It lists all the collections available inside sample_training database. We can see the following collections:

companies
grades
inspections
posts
routes
trips
zips

find() command example

In this section, we will learn to use the find() command.

find command with default query

The default query is not to specify a query as a parameter in find() method.

The syntax for executing find() command is:

db.collectionName.find()

To execute any query in MongoDB first, we need to go to the database by executing the use databaseName command. Hence, in our query, we don’t need to specify a database name.

In the above syntax, collectionName is the actual collection name that we want to see the data from. find is the method where we will be sending a query as a parameter. Let’s see the following example where we can see all the cities available in zips collection where state is NY.

 db.zips.find()

It lists all the data below:

find() command in MongoDB query zips collection 1

The document is listed but it does not look good to read. To make it look better we can execute a pretty() method followed by find()

Example:

 db.zips.find().pretty()

The output is:

find() command in MongoDB pretty method along with find

Now, we can see that the output looks better, isn’t it? 🙂

find command with custom query

We can pass any valid JSON as a query in MongoDB.

In this example, we can execute a find command to filter all the zips where the state is NY.

db.zips.find({"state":"NY"})

We can see the output that only the state NY is listed.

find() command in MongoDB New York stage zips

By default, MongoDB will list maximum 20 documents from the collection. Hence, to see more data from the query we just need to type it and it will list another 20 documents and so on.

find command with count

In the earlier example, we queried the collection but we don’t know the exact number of documents available. To count the documents inside the collection we can use count() method.

There are two ways to execute a count command or method.

If we have to count all the documents in a collection without filter then:

db.zips.count()

It counts all the available documents in the zips collection.

If we have to count documents along with the filter query then:

db.zips.find({"state":"NY"}).count()

It counts all the zips where the state is “NY”.

Summary

  • Use show dbs and show collections to list available namespaces (databases and collections)
  • find() returns a cursor with documents that match the find query. It lists only 20 documents from the collection without any specified order. We can see more data by typing it in the shell.
  • count() returns the number of documents that match the find query.
  • pretty() formats the documents in the cursor

Reference: MongoDB University Course

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments