
The MongoDB Find and Find One commands locate documents that match search parameters. These commands can also be used with Python or Node.js.
MongoDB Find: what is it and what is it for?
If MongoDB is familiar to you, you already know that the database management system works a little differently from traditional relational databases, such as MySQL. Of course, MongoDB also allows you to create databases. However, this data is stored in documents, not tables, which are in turn grouped into collections. In such a configuration, how find and view data corresponding? For this, there are two MongoDB commands: Find and Find One. They create a cursor to documents that match the defined search criteria.
Find Command Syntax
To understand how the Find command works, start by taking a look at its basic syntax:
db.collection.find
(
<query>,</query>
<projection></projection>
)
Find also includes two other parameters noted in square brackets.
Find command example
For example, suppose you want to search through a list of customers in France using MongoDB’s Find command. This list is in the customer database. To list all documents, use the Find command without specifying any
>use clients
switched to db clients
>db.liste_france.find();
>
The result is the complete list of French customers, with all the information recorded. Documents are always preceded by their unique identifier _idwhich allows the system to identify each document precisely.
Sample Find Command with Parameters
While it can indeed be useful to list all documents using the Find command, it becomes much more interesting if you want filter only specific documents. This is what the parameters mentioned above are for. Imagine that the following information is stored for each customer and can be displayed on demand:
"_id": ObjectID,
"name": "Martin"
"adress": "152 rue des Pyrénées"
"city": "Paris"
If your database contains several hundred customers from different cities and you only want to filter, for example, those living in Paris, here is how to do it:
Db.liste_france.find({"city": "Paris"})
The system is now filtering all entries in the “liste_france” collection and only displays results whose city is Paris.
Sample Find-One Command
Alongside the general Find option, which allows you to display all or part of the documents, the system also offers the additional MongoDB Find One function. This method displays exactly one document corresponding to the search criteria. The first document in the list is selected. If no document matches the search criteria, the result is « null » is displayed. The syntax of Find One resembles that of Find:
db.collection.findOne
(
<query>,</query>
<projection></projection>
)
Also in this case,
>use clients
switched to db clients
>db.liste_france.findOne();
>
In this case, the output will be the first document in your France customer list.
MongoDB Find command with Node.js and Python
It is also possible to use MongoDB with Node.js or Python. In this case, the syntax of the Find command is a little different, while maintaining the same basic functionalities.
Find One command with Python
One of the most popular MongoDB distributions under Python is PyMongo. If this is the one you are using, the Find One command will have the following syntax:
import pymongo
myclient = pymongo.MongoClient ("mongodb://localhost:24137/")
mydb = myclient ["clients"]
mycol = mydb ["liste_france"]
x = myco.find_one()
print ( x )
Find-One command with Node.js
The Node.js JavaScript runtime can also be used in MongoDB. If you want to run the Find One command there, here’s what the code looks like:
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 ("liste_france").findOne ( {}, function (err, result) {
if (err) throw err;
console-log (result.name);
db.close();
} );
} );
Advice
Looking to sort your documents in MongoDB for more readability? So save time with the MongoDB Sort command!