Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
MongoDBis oneof the most popular NoSQL databases. MongoDB data is stored in BSON format as key-value pairs where we define a unique key with a value associated with it. These key-value pairs are then stored in a document, which in turn is stored in a collection.
We use the MongoDB query documents method to find documents from a collection in the database. Let us discuss them in detail.
MongoDB query
MongoDB Query is a way to find the document from the MongoDB database. MongoDB query provides a way for fetching documents from the database like SQL(Structured Query Language) queries in SQL.
While performing a MongoDB query operation, we can use criteria or multiple criteria, which are then used to retrieve specific documents from the database.
MongoDB query documents: find() Method
In MongoDB, the find() method is used to find documents in a collection. It returns a cursor to the retrieved documents and returns one by one. If we want to return the pointer on all documents, use an empty() parameter that returns all documents.
It specifies the fields to be returned in the documents that match the selection criteria. To return all fields in the matching documents, exclude this parameter.
document
Return:
It returns a cursor to the documents that match the selection criteria.
Examples:
// Finds all the documents with the age 21
> db.students.find({age: 21})
{ "_id" : ObjectId("61b593fb0b9150cdf9cdf095"), "name" : "Rahul", "age" : 21 }
{ "_id" : ObjectId("61b594380b9150cdf9cdf096"), "name" : "Raj", "age" : 21 }
// To display only the name field use the projection parameter
> db.students.find({},{name:1, _id: 0})
{ "name" : "Sapna" }
{ "name" : "Rahul" }
{ "name" : "Raj" }
MongoDB query documents: findOne() Method
In MongoDB, the findOne() method is used to find one document in a collection. It returns only one document that matches the given selection criteria.
It specifies the fields to be returned in the documents that match the selection criteria.
document
Return:
It returns only one document that matches the given selection criteria. If multiple documents satisfy the given selection criteria, this method will return the first matching document according to the natural order. If no document matches the selection criteria, this method will return null.
Example:
// To find the first document with the age 21
> db.students.findOne({age: 21})
{
"_id" : ObjectId("61b593fb0b9150cdf9cdf095"),
"name" : "Rahul",
"age" : 21
}
$eq filter Query
The equality operator is used to match the documents where the field's value is equal to the specified value.
Syntax:
> db.COLLECTION_NAME.find({key: {$eq: value}})
Example:
// Find document with the name Sapna
> db.students.find({name:{$eq:"Sapna"}})
{ "_id" : ObjectId("61b5ddb911030d0eb6e22192"), "name" : "Sapna", "age" : 20 }
$gte and $lte filter Query
Using conditions like greater than equal or less than equal, use the $gte or $lte operator respectively in the find() method to get the specific numeric data.
Syntax:
// Greater than or equal to
> db.COLLECTION_NAME.find({key: {$gte: value}})
// Less than or equal to
> db.COLLECTION_NAME.find({key: {$lte: value}})
Example:
// Age greater than or equal to 21
> db.students.find({age: {$gte: 21}})
{ "_id" : ObjectId("61b5de5611030d0eb6e22193"), "name" : "Rahul", "age" : 21 }
{ "_id" : ObjectId("61b5de6811030d0eb6e22194"), "name" : "Raj", "age" : 21 }
$exists filter Query
It shows all the documents in the collection if they exist on a given key.
// Find all the documents which has the field name
> db.students.find({name: {$exists: true}})
{ "_id" : ObjectId("61b5ddb911030d0eb6e22192"), "name" : "Sapna", "age" : 20 }
{ "_id" : ObjectId("61b5de5611030d0eb6e22193"), "name" : "Rahul", "age" : 21 }
{ "_id" : ObjectId("61b5de6811030d0eb6e22194"), "name" : "Raj", "age" : 21 }
Frequently Asked Questions
1. How to limit the documents while using the query methods?
Ans:- To limit the documents, we use the limit method. For example,
// To limit the documents to 1
> db.students.find({}).limit(1)
{ "_id" : ObjectId("61b593f00b9150cdf9cdf094"), "name" : "Sapna", "age" : 20 }
2. What is the use of $and operator in MongoDB?
Ans:- It performs logical AND operation on the array of expressions and only returns those documents that match all the given expressions in the array. For example,
// Finds the document with name Sapna and age 20
> db.students.find({$and:[{"name":"Sapna"},{"age": 20}]})
{ "_id" : ObjectId("61b5ddb911030d0eb6e22192"), "name" : "Sapna", "age" : 20 }
3. How to sort the returned documents?
Ans:- The sort() method is used to order the documents in the result set. For example,
Ans:- The $in operator is used to select the documents where the value of a field equals any value in the specified array. For example,
// Finds documents where the name is Sapna or Raj
> db.students.find({ name: {$in: ["Sapna", "Raj"]}})
{ "_id" : ObjectId("61b5ddb911030d0eb6e22192"), "name" : "Sapna", "age" : 20 }
{ "_id" : ObjectId("61b5de6811030d0eb6e22194"), "name" : "Raj", "age" : 21 }
5. Mention some comparison operators available in MongoDB.
Ans:- Some common comparison operators used in MongoDB are:- $gt(greater than), $lt(less than), $nin(not in), $not, $ne (not equal to), $all, etc.
Key Takeaways
This blog covered the usage of the MongoDB query documents methods in detail. The methods discussed in this blog are: find() and findOne(). We have also discussed the various operators and methods used along with the find() method.
Don't stop here. Check out more blogs related to MongoDB. Also, if you are a beginner in MongoDB, go through the 25 Most Common Commands for MongoDB Beginners.
We hope you found this blog useful. Liked the blog? Then feel free to upvote and share it.