Last Updated: 14 August, 2023
In MongoDB, we can retrieve or query one or more documents from the collection using the following methods:
Method Name | Description |
---|---|
db.collection.findOne(query, projection, options) | Returns a single document from a collection or view that satisfies the specified query criteria |
db.collection.find(query, projection, options) | Returns a cursor to the documents that match the query criteria. |
We can specify the condition for retrive operations, which means we retrieve some specific types of documents from the collection based on the requirement.
In MongoDB, All write operations are atomic at the level of a single document.
Let's see examples of retrieve documents from a collection in MongoDB.
For example, suppose we have a collection named student which has the following documents as given below:
[ {sid: 1, name: 'Ramesh', class: 'Nursery', roll: 15, gender: 'M'}, {sid: 2, name: 'Gyatri', class: 'LKG', roll: 18, gender: 'F'}, {sid: 3, name: 'John', class: 'Nursery', roll: 26, gender: 'M'}, {sid: 4, name: 'Priya', class: 'UKG', roll: 10, gender: 'F'}, {sid: 5, name: 'Suraj', class: 'LKG', roll: 12, gender: 'M'} ]
The findOne() method returns a single document from a collection or view that satisfies the specified query criteria. If multiple documents satisfy the query, this method returns the first document according to the natural order, which reflects the order of documents on the disk.
This method will return null if no document satisfies the query.
let's fetch the document from the student collection using the findOne() method.
Example 1: Use findOne() method without criteria
db.student.findOne({});
It returns the first document from a collection as there is no criteria specified.
Output
{ "_id" : ObjectId("64d7b4d43e4e00724be5436d"), "sid" : 1, "name" : "Ramesh", "class" : "Nursery", "roll" : 15, "gender" : "M" }
Example 2: Use findOne() method with criteria
db.student.findOne({class: 'Nursery'});
Here, It returns first document from student collection whose class name is Nursery.
Output
{ "_id" : ObjectId("64d7b4d43e4e00724be5436d"), "sid" : 1, "name" : "Ramesh", "class" : "Nursery", "roll" : 15, "gender" : "M" }
Example 3: Finding non existing document
db.student.findOne({roll: 22});
It will return null if no document satisfies the query.
Output
null
The find() method is used to fetch one or more documents from a collection or view in MongoDB. This method takes two parameters named query and projection. Both are parameters are optional.
This method returns a cursor to the documents that match the query criteria.
Example 4: Select/Fetch all documents in a Collection
db.student.find({});
This operation uses a filter predicate of {}, which corresponds to the following SQL statement: SELECT * FROM student;
Output
/* 1 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436d"), "sid" : 1, "name" : "Ramesh", "class" : "Nursery", "roll" : 15, "gender" : "M" } /* 2 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436e"), "sid" : 2, "name" : "Gyatri", "class" : "LKG", "roll" : 18, "gender" : "F" } /* 3 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436f"), "sid" : 3, "name" : "John", "class" : "Nursery", "roll" : 26, "gender" : "M" } /* 4 */ { "_id" : ObjectId("64d7b4d43e4e00724be54370"), "sid" : 4, "name" : "Priya", "class" : "UKG", "roll" : 10, "gender" : "F" } /* 5 */ { "_id" : ObjectId("64d7b4d43e4e00724be54371"), "sid" : 5, "name" : "Suraj", "class" : "LKG", "roll" : 12, "gender" : "M" }
Example 5: Specifing criteria in find() method
db.student.find({class: 'LKG'});
Here, we have specified criteria for find() method. It is same as WHERE clause in SQL query.
Output
/* 1 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436e"), "sid" : 2, "name" : "Gyatri", "class" : "LKG", "roll" : 18, "gender" : "F" } /* 2 */ { "_id" : ObjectId("64d7b4d43e4e00724be54371"), "sid" : 5, "name" : "Suraj", "class" : "LKG", "roll" : 12, "gender" : "M" }
Example 6: Specifing AND condition in find() method
db.student.find({name: 'Gyatri', class: 'LKG'});
Here, specified two criteria for fetching documents. This corresponds to the SQL statement: SELECT * FROM student WHERE name = "Gaytri" AND class = "LKG";
Output
/* 1 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436e"), "sid" : 2, "name" : "Gyatri", "class" : "LKG", "roll" : 18, "gender" : "F" }
Example 7: Specifing OR condition in find() method
For the 'OR' condition, we need to use $or operator. See the below example.
db.student.find({$or:[{name: 'Gyatri'}, {class: 'LKG'}]});
Here, specified two criteria with $or operator. This corresponds to the SQL statement: SELECT * FROM student WHERE name = "Gaytri" OR class = "LKG";
Output
/* 1 */ { "_id" : ObjectId("64d7b4d43e4e00724be5436e"), "sid" : 2, "name" : "Gyatri", "class" : "LKG", "roll" : 18, "gender" : "F" } /* 2 */ { "_id" : ObjectId("64d7b4d43e4e00724be54371"), "sid" : 5, "name" : "Suraj", "class" : "LKG", "roll" : 12, "gender" : "M" }
MongoDB provides a rich set of query operators that can be used to compare and reference document fields.
There are following types of Comparison operators in MongoDB, it returns data based on value comparisons.
Operator Name | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$in | Matches any of the values specified in an array. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$nin | Matches none of the values specified in an array. |
Logical operators return data based on expressions that evaluate to true or false.
Operator Name | Description |
---|---|
$and | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
$not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
$nor | Joins query clauses with a logical NOR returns all documents that fail to match both clauses. |
$or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
Element operators return data based on field existence or data types.
Operator Name | Description |
---|---|
$exists | Matches documents that have the specified field. |
$type | Selects documents if a field is of the specified type. |
MongoDB also provides many other operations such as Evaluation Query Operators, Geospatial Query Operators, Array Query Operators, Bitwise Query Operators, Projection Operators etc.
Example 8: find() method with '$gt' operator
db.student.find({roll: {$gt: 15}});
Output
/* 1 */ { "_id" : ObjectId("64d7d51e3e4e00724be54373"), "sid" : 2, "name" : "Gyatri", "class" : "LKG", "roll" : 18, "gender" : "F" } /* 2 */ { "_id" : ObjectId("64d7d51e3e4e00724be54374"), "sid" : 3, "name" : "John", "class" : "Nursery", "roll" : 26, "gender" : "M" }
Example 9: find() method with '$lt' operator
db.student.find({roll: {$lt: 15}});
Output
/* 1 */ { "_id" : ObjectId("64d7d51e3e4e00724be54375"), "sid" : 4, "name" : "Priya", "class" : "UKG", "roll" : 10, "gender" : "F" } /* 2 */ { "_id" : ObjectId("64d7d51e3e4e00724be54376"), "sid" : 5, "name" : "Suraj", "class" : "LKG", "roll" : 12, "gender" : "M" }
MongoDB supports regular expressions $regex queries to perform string pattern matches.
Example 10: find() method with '$regex' operator
db.student.find({name: {$regex: '^p'}})
Output
/* 1 */ { "_id" : ObjectId("64d7d51e3e4e00724be54375"), "sid" : 4, "name" : "Priya", "class" : "UKG", "roll" : 10, "gender" : "F" }
Reference: https://www.mongodb.com/docs/mongodb-shell/crud/read/
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com