Finding Documents in MongoDB: A Comprehensive Guide to the find() Method
Learn how to effectively retrieve documents from MongoDB collections using the find() method. Explore the various query operators and techniques to filter, sort, and limit your search results. This guide will provide you with the necessary knowledge to efficiently extract data from your MongoDB databases.
MongoDB: Find Documents in Collection using find()
In MongoDB, collections are like tables in RDBMS, and documents are like records. The find()
method retrieves one or more documents from a collection based on specified criteria.
MongoDB provides two methods for finding documents:
findOne()
- Returns the first document that matches the specified criteria.find()
- Returns a cursor to all documents that match the specified criteria.
Sample Data
Insert the following documents into the employees
collection:
Insert Sample Data
db.employees.insertMany([
{ _id: 1, firstName: "John", lastName: "King", email: "john.king@abc.com", salary: 5000, skills: [ "Angular", "React", "MongoDB" ], department: { "name": "IT" } },
{ _id: 2, firstName: "Sachin", lastName: "T", email: "sachin.t@abc.com", salary: 8000, skills: [ "Accounting", "Tax" ], department: { "name": "Finance" } },
{ _id: 3, firstName: "James", lastName: "Bond", email: "jamesb@abc.com", salary: 7500, skills: [ "Sales", "Marketing" ], department: { "name": "Marketing" } },
{ _id: 4, firstName: "Steve", lastName: "J", email: "steve.j@abc.com", salary: 7000 },
{ _id: 5, firstName: "Kapil", lastName: "D", email: "kapil.d@abc.com", salary: 4500, skills: [ "Accounting", "Tax" ], department: { "name": "Finance" } },
{ _id: 6, firstName: "Amitabh", lastName: "B", email: "amitabh.b@abc.com", salary: 7000 }
])
Using find()
The find()
method returns a cursor to the documents that match the specified criteria. If no criteria are provided, it returns all documents in the collection.
Syntax
db.collection.find(query, projection)
Parameters:
query
: Optional. Criteria for finding documents.projection
: Optional. Fields to include or exclude in the result.
Examples
1. Retrieve all documents in the employees
collection:
Example: Find All Documents
db.employees.find()
Output:
[ { _id: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', salary: 5000, skills: [ 'Angular', 'React', 'MongoDB' ], department: { name: 'IT' } }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } }, { _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', salary: 7000 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 } ]
2. Find documents with a salary of 7000:
Example: Find Documents with Salary of 7000
db.employees.find({ salary: 7000 })
Output:
[ { _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', salary: 7000 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 } ]
3. Find documents where salary is greater than 7000:
Example: Find Documents with Salary Greater than 7000
db.employees.find({ salary: { $gt: 7000 } })
Output:
[ { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } } ]
4. Find documents with salary between 7000 and 8000:
Example: Find Documents with Salary Between 7000 and 8000
db.employees.find({ salary: { $gt: 7000, $lt: 8000 } })
Output:
[ { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } } ]
5. Find documents with a specific department name:
Example: Find Documents by Department Name
db.employees.find({ "department.name": "Finance" })
Output:
[ { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } } ]
6. Find documents based on array elements:
Example: Find Documents by Array Element
db.employees.find({ "skills": "Tax" }) // Finds documents where skills contain "Tax"
db.employees.find({ "skills": { $in: [ "Tax", "Sales" ] } }) // Finds documents where skills contain "Tax" or "Sales"
db.employees.find({ "skills": { $all: [ "Tax", "Accounting" ] } }) // Finds documents where skills contain both "Tax" and "Accounting"
db.employees.find({ "skills": { $size: 3 } }) // Finds documents where skills array has 3 elements
7. Retrieve specific fields using projection:
Example: Find Documents with Projection
db.employees.find({ salary: 7000 }, { firstName: 1, lastName: 1 })
Output:
[ { _id: 4, firstName: 'Steve', lastName: 'J' }, { _id: 6, firstName: 'Amitabh', lastName: 'B' } ]
8. Exclude the _id
field in the output:
Example: Find Documents Excluding _id Field
db.employees.findOne({ firstName: "Sachin" }, { _id: 0, firstName: 1, lastName: 1 })
Output:
{ firstName: 'Sachin', lastName: 'T' }
Note: By default, the _id
field is included in the results. To exclude it, specify { _id: 0 }
in the projection.