MongoDB Query Documents
Introduction
MongoDB provides powerful methods and operators for querying documents in a collection. Understanding these query methods and operators is essential for retrieving and managing data effectively. In this article, we will explore the main query methods and operators used in MongoDB.
Query Methods
find()
The find()
method is used to retrieve multiple documents from a collection that match the specified query criteria. It returns a cursor, which allows you to iterate over the results.
db.users.find({ "age": { "$gte": 18 } })
findOne()
The findOne()
method retrieves a single document from a collection that matches the specified query criteria. It returns the first document that matches the query.
db.users.findOne({ "username": "john_doe" })
Query Operators
$eq
(Equal)
The $eq
operator is used to match documents where the value of a field is equal to a specified value.
db.products.find({ "category": { "$eq": "electronics" } })
$ne
(Not Equal)
The $ne
operator matches documents where the value of a field is not equal to the specified value.
db.orders.find({ "status": { "$ne": "completed" } })
$gt
(Greater Than)
The $gt
operator finds documents where the value of a field is greater than a specified value.
db.products.find({ "price": { "$gt": 100 } })
$lt
(Less Than)
The $lt
operator matches documents where the value of a field is less than a specified value.
db.products.find({ "price": { "$lt": 50 } })
$gte
(Greater Than or Equal To)
The $gte
operator retrieves documents where the value of a field is greater than or equal to a specified value.
db.products.find({ "price": { "$gte": 50 } })
$lte
(Less Than or Equal To)
The $lte
operator matches documents where the value of a field is less than or equal to a specified value.
db.products.find({ "price": { "$lte": 100 } })
$in
The $in
operator is used to find documents where the value of a field is within a specified array of values.
db.products.find({ "category": { "$in": ["electronics", "furniture"] } })
$nin
(Not In)
The $nin
operator matches documents where the value of a field is not within a specified array of values.
db.products.find({ "category": { "$nin": ["outdoor", "garden"] } })
$and
The $and
operator combines multiple query conditions where all conditions must be met.
db.products.find({ "$and": [{ "price": { "$gte": 50 } }, { "stock": { "$gt": 0 } }] })
$or
The $or
operator combines multiple query conditions where at least one condition must be met.
db.products.find({ "$or": [{ "price": { "$lt": 20 } }, { "category": "clearance" }] })
$not
The $not
operator negates a query condition, matching documents that do not satisfy the specified condition.
db.products.find({ "price": { "$not": { "$gte": 100 } } })
$exists
The $exists
operator checks for the presence or absence of a field in the document.
db.products.find({ "discount": { "$exists": true } })
$regex
The $regex
operator performs pattern matching using regular expressions.
db.users.find({ "email": { "$regex": "@example.com$" } })
$all
The $all
operator matches documents where an array field contains all specified elements.
db.orders.find({ "items": { "$all": ["item1", "item2"] } })
$elemMatch
The $elemMatch
operator is used to query documents with arrays, matching documents that contain at least one array element that matches all specified criteria.
db.orders.find({ "items": { "$elemMatch": { "name": "item1", "quantity": { "$gte": 2 } } } })
Sorting Results
sort()
The sort()
method is used to sort the results of a query based on specified fields. You can sort documents in ascending or descending order.
db.products.find().sort({ "price": 1 })
Additional Query Methods
limit()
The limit()
method restricts the number of documents returned by a query. This is useful for paginating results.
db.products.find().limit(5)
skip()
The skip()
method skips a specified number of documents in the result set. It is often used in conjunction with limit()
for pagination.
db.products.find().skip(10).limit(5)
count()
The count()
method counts the number of documents that match the query criteria. It provides a count of matching documents without returning the documents themselves.
db.products.find({ "category": "electronics" }).count()
Conclusion
MongoDB offers a range of query methods and operators that allow for flexible and powerful data retrieval. By understanding and using these methods and operators effectively, you can efficiently query and manage your MongoDB collections to meet your application's needs.