Basic Queries in MongoDB
Basic Queries in MongoDB
Basic queries in MongoDB allow you to retrieve documents from a collection based on specified criteria. MongoDB’s query language is similar to JavaScript and provides a rich set of operators for filtering and retrieving data.
Query Syntax
MongoDB queries are structured as JSON-like documents. The basic syntax for querying documents is:
db.collection.find(query, projection)
- query: Specifies the criteria for selecting documents.
- projection: Specifies which fields to return (optional).
Basic Query Operations
- Find All Documents
- Find Documents by Criteria
- Query Operators
- Combining Conditions
- Projections
1. Find All Documents
To retrieve all documents from a collection, use the find()
method without any query criteria.
Syntax:
db.collection.find()
Example:
db.users.find()
This command returns all documents in the users
collection.
2. Find Documents by Criteria
To find documents that match specific criteria, use a query document that specifies the search conditions.
Syntax:
db.collection.find(query)
Example:
Find users with the name "John Doe":
db.users.find({ name: "John Doe" })
This command returns all documents in the users
collection where the name
field is "John Doe".
3. Query Operators
MongoDB supports a variety of query operators to specify more complex criteria:
- Comparison Operators:
$eq
: Equal to$ne
: Not equal to$gt
: Greater than$lt
: Less than$gte
: Greater than or equal to$lte
: Less than or equal to
Syntax:
db.collection.find({ field: { $operator: value } })
Example:
Find users older than 25:
db.users.find({ age: { $gt: 25 } })
Find products with a price less than or equal to 100:
db.products.find({ price: { $lte: 100 } })
- Logical Operators:
$and
: Combines multiple conditions (implicit with multiple query fields)$or
: Matches documents that satisfy at least one condition$not
: Inverts the query$nor
: Matches documents that do not satisfy any of the conditions
Syntax:
db.collection.find({ $or: [ { condition1 }, { condition2 } ] })
Example:
Find users who are either named "John Doe" or are older than 30:
db.users.find({ $or: [ { name: "John Doe" }, { age: { $gt: 30 } } ] })
- Element Operators:
$exists
: Checks for the existence of a field$type
: Checks the type of a field
Syntax:
db.collection.find({ field: { $exists: true } })
Example:
Find documents where the email
field exists:
db.users.find({ email: { $exists: true } })
4. Combining Conditions
You can combine multiple query conditions to create more specific queries. MongoDB queries use implicit $and
when specifying multiple conditions.
Syntax:
db.collection.find({ field1: value1, field2: value2 })
Example:
Find users who are named "John Doe" and are older than 25:
db.users.find({ name: "John Doe", age: { $gt: 25 } })
5. Projections
Projections control which fields are returned in the query results. By default, all fields are returned, but you can specify fields to include or exclude.
Syntax:
db.collection.find(query, projection)
- Include Fields:
{ field: 1 }
: Include the specified field(s)
- Exclude Fields:
{ field: 0 }
: Exclude the specified field(s)
Syntax:
db.collection.find(query, { field1: 1, field2: 1 })
Example:
Find all users but only return the name
and age
fields:
db.users.find({}, { name: 1, age: 1 })
Example:
Find all users but exclude the email
field:
db.users.find({}, { email: 0 })
Example Queries
Find all users with a specific email:
db.users.find({ email: "johndoe@example.com" })
Find products with a price between 50 and 150:
db.products.find({ price: { $gte: 50, $lte: 150 } })
Find users who are not older than 25:
db.users.find({ age: { $lte: 25 } })
Find documents with a field that is not null:
db.users.find({ email: { $ne: null } })