NoSQL (Not Only SQL) is a non-relational database that stores data in flexible formats instead of traditional tables. It is designed to handle large volumes of structured, semi-structured and unstructured data efficiently.
- Data is commonly stored as documents, key-value pairs, columns or graphs.
- Documents are typically stored in JSON-like formats.
NoSQL Query Syntax
NoSQL databases use filters, logical operators and conditional operators to retrieve documents that match specific criteria. A query consists of a collection name and a set of conditions that determine which documents should be returned.
Basic Query Structure
The basic structure of a NoSQL query is:
db.collection.find({ condition })Here:
- db represents the database.
- collection represents the collection containing the documents.
- find() is used to retrieve documents.
- condition specifies the filtering criteria.
Filtering Operators
Filtering operators are used to apply conditions and retrieve matching documents. The commonly used filtering operators are listed below.
| Operator | Description | Syntax |
|---|---|---|
| Field Match | Matches a single field value. | { "field": "value" } |
| Multiple Field Match | Matches multiple field values. | { "field1": "value1", "field2": "value2" } |
| $regex | Matches a regular expression. | { "field": { "$regex": "pattern" } } |
| $startsWith | Matches values starting with a string. | { "field": { "$startsWith": "text" } } |
| $endsWith | Matches values ending with a string. | { "field": { "$endsWith": "text" } } |
Field Match
Field matching is used to retrieve documents where a field contains a specific value.
db.users.find({ name: "John" })Multiple Field Match
Multiple fields can be matched by specifying more than one field-value pair in a query.
db.users.find({
name: "John",
city: "Delhi"
})
This query is equivalent to:
db.users.find({
$and: [
{ name: "John" },
{ city: "Delhi" }
]
})
Match Sub Fields
Sub-fields in embedded documents can be queried using dot notation.
db.users.find({
"address.city": "Delhi"
})
$regex Operator
The $regex operator matches field values against a regular expression pattern.
db.users.find({
name: { $regex: "^Jo" }
})
$startsWith Operator
The $startsWith operator returns documents whose field values begin with a specified string.
db.users.find({
name: { $startsWith: "Jo" }
})
$endsWith Operator
The $endsWith operator returns documents whose field values end with a specified string.
db.users.find({
name: { $endsWith: "hn" }
})
Logical Operators
Logical operators are used to combine multiple query conditions in a NoSQL query.The following table summarizes the commonly used logical operators and their corresponding query syntax.
| Operator | Description | Syntax |
|---|---|---|
| $and | Returns documents that satisfy all specified conditions. | { $and: [condition1, condition2, ...] } |
| $or | Returns documents that satisfy at least one condition. | { $or: [condition1, condition2, ...] } |
| $not | Returns documents that do not match a specified condition. | { field: { $not: condition } } |
| $in | Returns documents whose field value matches any value in a list. | { field: { $in: [value1, value2, ...] } } |
| $nin | Returns documents whose field value does not match any value in a list. | { field: { $nin: [value1, value2, ...] } } |
$and Operator
The $and operator returns documents that satisfy all specified conditions.
db.users.find({
$and: [
{ city: "Delhi" },
{ age: 25 }
]
})
$or Operator
The $or operator returns documents that satisfy at least one of the specified conditions.
db.users.find({
$or: [
{ city: "Delhi" },
{ city: "Mumbai" }
]
})
$not Operator
The $not operator returns documents that do not match a specified condition.
db.users.find({
status: { $not: "Inactive" }
})
$in Operator
The $in operator returns documents whose field value matches any value in the specified array.
db.users.find({
city: {
$in: ["Delhi", "Mumbai"]
}
})
$nin Operator
The $nin operator returns documents whose field value does not match any value in the specified array.
db.users.find({
city: {
$nin: ["Delhi", "Mumbai"]
}
})
Conditional Operators
Conditional operators are used to compare field values and filter documents based on specific conditions. The following table summarizes the commonly used conditional operators and their query syntax.
| Operator | Description | Syntax |
|---|---|---|
| $gt | Matches values greater than a specified value. | { "field": { "$gt": value } } |
| $gte | Matches values greater than or equal to a specified value. | { "field": { "$gte": value } } |
| $lt | Matches values less than a specified value. | { "field": { "$lt": value } } |
| $lte | Matches values less than or equal to a specified value. | { "field": { "$lte": value } } |
| $ne | Matches values that are not equal to a specified value. | { "field": { "$ne": value } } |
| $exists | Checks whether a field exists in a document. | { "field": { "$exists": true } } |
| $elemMatch | Matches at least one element in an array that satisfies the specified condition. | { "arrayField": { "$elemMatch": { condition } } } |
| $date | Filters documents based on date values. | { "field": { "$gt": { "$date": "YYYY-MM-DD" } } } |
$gt Operator
The $gt operator returns documents whose field value is greater than the specified value.
db.users.find({
salary: { $gt: 50000 }
})
$gte Operator
The $gte operator returns documents whose field value is greater than or equal to the specified value.
db.users.find({
salary: { $gte: 50000 }
})
$lt Operator
The $lt operator returns documents whose field value is less than the specified value.
db.users.find({
salary: { $lt: 50000 }
})
$lte Operator
The $lte operator returns documents whose field value is less than or equal to the specified value.
db.users.find({
salary: { $lte: 50000 }
})
$ne Operator
The $ne operator returns documents whose field value is not equal to the specified value.
db.users.find({
status: { $ne: "Inactive" }
})
$exists Operator
The $exists operator checks whether a field exists in a document.
db.users.find({
email: { $exists: true }
})
$elemMatch Operator
The $elemMatch operator matches documents containing at least one array element that satisfies the specified condition.
db.users.find({
skills: {
$elemMatch: {
name: "Python"
}
}
})
$date Operator
The $date operator is used to filter documents based on date values.
db.users.find({
createdAt: {
$gt: {
$date: "2024-01-01"
}
}
})