Finding Documents in MongoDB
Introduction
In MongoDB, finding documents is done using the find()
method, which retrieves documents from a collection based on specified criteria. This tutorial will guide you through the various ways to find documents in a MongoDB collection using mongosh
.
Prerequisites
MongoDB installed and running on your machine.
Access to
mongosh
and a populated database.
Step 1: Start mongosh
mongosh
Open Terminal/Command Prompt:
Launch your terminal (macOS/Linux) or command prompt (Windows).
Start
mongosh
:Type the following command and press Enter:
Step 2: Switch to the Database
Switch to the database where your collection is located.
Use a Database:
Step 3: Finding Documents
1. Find All Documents
To retrieve all documents in a collection, use the find()
method without any parameters.
This will return all documents in the
users
collection.
2. Find Documents with Criteria
You can specify criteria to filter the documents returned by the find()
method.
Find Documents by Field Value:
To find documents where the name is "Pankaj":
Find Documents by Multiple Fields:
To find documents where the age is 30 and the city is "Indore":
3. Find Documents with Comparison Operators
MongoDB supports various comparison operators that you can use in your queries.
Find Documents with
$gt
(Greater Than):To find users older than 25:
Find Documents with
$lt
(Less Than):To find users younger than 30:
Find Documents with
$ne
(Not Equal):To find users not living in "Mumbai":
4. Find Documents with Logical Operators
MongoDB allows you to combine multiple criteria using logical operators.
Using
$or
:To find users who are either from "Delhi" or "Mumbai":
Using
$and
:To find users who are older than 25 and from "Indore":
5. Find Documents with Projection
You can limit the fields returned by the query using projection.
Find Specific Fields:
To find all users but only return their names and cities:
6. Find One Document
To find a single document that matches the criteria, use the findOne()
method.
This will return the first document that matches the criteria.
Step 4: Sort and Limit Results
You can sort and limit the results returned by your queries.
Sort Results:
To find all users sorted by age in ascending order:
Limit Results:
To limit the results to the first 2 documents:
Conclusion
You have learned how to find documents in MongoDB using mongosh
. You can retrieve all documents, filter them based on specific criteria, use comparison and logical operators, project specific fields, and sort and limit the results.
Feel free to reach out with any questions or for further guidance!
Last updated