We will use the database that we created at last session. You can read this article and reproduce it in your local environment.
The find()
is a powerful tool that empowers us to execute diverse selections based on specific criteria.
- Find all documents in a collection
- Find documents matching a specific condition
- Find documents where a field matches a regular expression
- Find documents where a field matches any value in an array
- Find documents where a field matches multiple conditions (AND)
- Find documents where a field matches multiple conditions (OR)
- Find documents and display specific field
1. Find all documents in a collection
#personnel is a collection name
db.personnel.find()
2.Find documents matching a specific condition
# Example 1: find the employee "Amanda Hall"'s data
> db.personnel.find({name:"Amanda Hall"})
< {
_id: ObjectId('66053f848ccb6407b4ba21cd'),
name: 'Amanda Hall',
age: 33,
working_experience: 9,
department: 'Quality Assurance',
salary: 69000,
skills: [
'Testing',
'Bug Tracking',
'Automation'
]
}
# Example 2: find the employees whose age are 35
> db.personnel.find({age:35})
< {
_id: ObjectId('66053f848ccb6407b4ba21c6'),
name: 'Michael Johnson',
age: 35,
working_experience: 8,
department: 'Finance',
salary: 70000,
skills: [
'Accounting',
'Financial Analysis',
'Excel'
]
}
3. Find documents where a field matches a regular expression
# Example 3: find the employee whose name is like "Jan"
> db.personnel.find({ name: /Jan/ })
< {
_id: ObjectId('66053f848ccb6407b4ba21c5'),
name: 'Jane Smith',
age: 28,
working_experience: 4,
department: 'Marketing',
salary: 55000,
skills: [
'Digital Marketing',
'SEO',
'Social Media Management'
]
}
4. Find documents where a field matches any value in an array
# Example 4: find the employee who has Python and Machine Learning skills
> db.personnel.find({ skills:{ $in:["Python","Machine Learning"]}})
< {
_id: ObjectId('66053f848ccb6407b4ba21cb'),
name: 'Michelle Martinez',
age: 31,
working_experience: 5,
department: 'Research and Development',
salary: 63000,
skills: [
'Research',
'Data Analysis',
'Python',
'Machine Learning'
]
}
{ skills: { $in: ["Python", "Machine Learning"] } }
It specifies that we want to find documents where the “skills” field matches any of the values listed in the array["Python", "Machine Learning"]
.
5. Find documents where a field matches multiple conditions (AND)
Example 5: find out the employee who has accounting skill AND in the finance department
> db.personnel.find({ skills:"Accounting", department:"Finance"})
< {
_id: ObjectId('66053f848ccb6407b4ba21c6'),
name: 'Michael Johnson',
age: 35,
working_experience: 8,
department: 'Finance',
salary: 70000,
skills: [
'Accounting',
'Financial Analysis',
'Excel'
]
}
6. Find documents where a field matches multiple conditions (OR)
Example 6: find out the employee who has Market Analysis skill OR in the finance department
> db.personnel.find({ $or: [{skills:"Market Analysis"}, {department:"Finance"}]})
< {
_id: ObjectId('66053f848ccb6407b4ba21c6'),
name: 'Michael Johnson',
age: 35,
working_experience: 8,
department: 'Finance',
salary: 70000,
skills: [
'Accounting',
'Financial Analysis',
'Excel'
]
}
{
_id: ObjectId('66053f848ccb6407b4ba21cc'),
name: 'Daniel Thompson',
age: 29,
working_experience: 4,
department: 'Product Management',
salary: 58000,
skills: [
'Product Development',
'Product Launch',
'Market Analysis'
]
}
{ $or: [...] }
: The operator means that the query should match documents that satisfy any of the conditions specified within the array.[{skills:"Market Analysis"}, {department:"Finance"}]
: This is an array containing two conditions.
7. Find documents and display specific field
Example 7: only show me the name and salary of each employee
#(It returns 10 result but here I only listed three)
> db.personnel.find({}, {name: 1, salary: 1})
< {
_id: ObjectId('66053f848ccb6407b4ba21c4'),
name: 'John Doe',
salary: 60000
}
{
_id: ObjectId('66053f848ccb6407b4ba21c5'),
name: 'Jane Smith',
salary: 55000
}
{
_id: ObjectId('66053f848ccb6407b4ba21c6'),
name: 'Michael Johnson',
salary: 70000
}
...
- The first parameter
{}
represents the filter criteria, which in this case is empty, meaning it will return all documents from thepersonnel
collection. - The second parameter
{name: 1, salary: 1}
specifies which fields to include in the result.1
indicates inclusion, while0
would indicate exclusion.