How to look for documents with MongoDB Find

Using MongoDB Find and Find One, you can locate documents that match your search parameters. These commands also work with Python or Node.js.

What is the MongoDB Find command and what is it used for?

If you’ve already spent a little time working with MongoDB, then you know that the database management system works differently than other traditional relational databases like MySQL. While MongoDB also creates databases, it saves data in documents instead of tables and then groups these documents into collections. Given this new way of storing information, you may be asking yourself how to search for data and how the relevant data is then displayed. In order to search for data, the system has two basic MongoDB commands: “find” and “find one”. These create a cursor on either a select group of documents or all documents that match your search criteria.

The find command syntax

To understand the find command and how it is used, let’s first take a look at the syntax:

db.collection.find
(
<query>,</query>
<projection></projection>
)

Within the find command, there are two additional parameters located in parentheses: <query> and <projection>. The first one, <query>, allows you to narrow your search down. The second one, <projection>, is optional and lets you determine which aspects should be displayed from the documents that fulfill the search criteria.

Examples of the find command

Imagine you would like to search through a list of clients from the United States using the MongoDB Find command. You can find this list as a collection in the “clients” database. To get a general overview of all the documents, use the find command and leave the <query> and <projection> parameters empty. The code will look as follows:

>use clients
switched to db clients
>db.list_unitedstates.find();
>

The result will be a complete list of clients in the United States, including all the information that you have entered for them. In front of all the documents, you will see a unique _iD Number, which the system uses to identify each document.

Examples of the find command with parameters

As useful as it can be to get an output of all documents by using the Find command, things get really interesting, when you filter for specific documents. You can use the parameters mentioned above to do this. Imagine that the following information is stored for each customer and can be displayed upon request:

"_id": ObjectID,
"name": "Smith"
"address": "17 Main Street"
"city": "Boston"

If you wanted to conduct a search for Portland-based clients in a database that contains hundreds of people from different cities, you could do the following:

Db.list_unitedstates.find({"city": "Portland"})

The system will now filter all entries in the collection “list_unitedstates”, showing you only those clients who are located in Portland.

Examples of the find one command

In addition to the general find option, which allows you to search for and display several documents that fulfill your search criteria, you can also use the additional MongoDB Find One feature. The first document that meets your specified criteria will be selected from a list and displayed. If there are no documents that meet the search criteria, the value “Null” will be displayed. The syntax for “find one” is similar to “find”:

db.collection.findOne
(
<query>,</query>
<projection></projection>
)

Search criteria are still defined using <query>, and the optional <projection> shows which fields of the matches will be displayed. If you use the find one command as per the example given above, it will look as follows:

>use clients
switched to db clients
>db.list_unitedstates.findOne();
>

The result displayed will be the first entry in your client list for the United States.

How to use the MongoDB find command in Node.js and Python

You can also use MongoDB with Node.js or Python. If you do this, the find command will look a little different, but the basic functionality will remain the same.

How to use the find one command in Python

PyMongo is one of the preferred MongoDB drivers if you work with Python. If you use this driver, the find one command will look as follows:

import pymongo
myclient = pymongo.MongoClient ("mongodb://localhost:24137/")
mydb = myclient ["clients"]
mycol = mydb ["list_unitedstates"]
x = myco.find_one()
print ( x )

How to use the find one command in Node.js

You can also use the JavaScript runtime environment Node.js in MongoDB. If you would like to run the find one command there, the code will look as follows:

var MongoClient = require ('mongodb').MongoClient;
var url = "mongodb://localhost:24137/";
MongoClient.connect (url, function (err, db) {
if (err) throw err;
var dbo = db.db ("mydb");
dbo.collection ("list_unitedstates").findOne ( {}, function (err, result) {
if (err) throw err;
console-log (result.name);
db.close();
} );
} );
Tip

Want to learn how to organize your documents in MongoDB so you can have a better overview of your data? Then follow the MongoDB sort command guide to learn how.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top