While basic CRUD operations form the bedrock of interacting with your data, understanding advanced operations in the MongoDB Query Language (MQL) can unlock powerful and flexible data manipulation capabilities in Polypheny. In this section, we will discuss various query operators, querying arrays and embedded documents, type checking and casting, and using regular expressions.
Query Operators
Query operators in MQL allow you to perform complex queries using special query operator symbols. They can be categorized into several types:
Comparison
Comparison operators allow you to compare different BSON values. Common comparison operators include $eq (equals), $gt (greater than), $gte (greater than or equals), $lt (less than), $lte (less than or equals), $ne (not equals), and $in (values in a specified array).
Here’s an example of a comparison operator:
db.collection.find({ "age" : { $gt : 30 } })
This query returns all documents where the age field is greater than 30.
Logical
Logical operators allow you to combine multiple query conditions. Common logical operators include $and, $not, $nor, and $or.
Here’s an example of a logical operator:
db.collection.find({ $and: [ { "age" : { $gt : 30 } }, { "name" : "John Doe" } ] })
This query returns all documents where the age field is greater than 30 and the name is “John Doe”.
Element
Element operators allow you to check the existence or data type of a field. They include $exists and $type.
Here’s an example of an element operator:
db.collection.find({ "email" : { $exists : true } })
This query returns all documents that have an email field.
Evaluation
Evaluation operators in MQL are used for performing various operations such as pattern matching, expressions, and schema validation. Here’s a breakdown of how these work:
$expr
The $expr operator allows you to use aggregation expression operators within your query language. Here’s an example:
db.collection.find({ $expr: { $gt: [ "$field1" , "$field2" ] } })
In this query, $expr compares field1 and field2 within the same document and returns those where field1 is greater than field2.
$jsonSchema
The $jsonSchema operator allows you to validate documents within a collection against the given JSON Schema:
db.collection.find({
$jsonSchema: {
bsonType: "object",
required: [ "name", "email" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType: "string",
description: "must be a string and is required"
}
}
}
})
This query returns documents that match the provided JSON Schema.
$mod
The $mod operator performs a modulo operation on the value of a field and selects documents with a specified result:
db.collection.find({ "age" : { $mod: [5, 0] } })
This query returns all documents where the age field is a multiple of 5.
$regex
The $regex operator is used to search for documents where a field matches the specified regular expression:
db.collection.find({ "name" : { $regex: /^J/, $options: 'i' } })
This query returns all documents where the name starts with “J”, irrespective of case.
$text
The $text operator performs a text search on the content of the fields indexed with a text index. A $text query can search for words, phrases, and more:
db.collection.find({ $text: { $search: "John Doe" } })
This query returns all documents that contain “John Doe” in any of the fields indexed with a text index.
Query Arrays
To query arrays, you can use a variety of approaches:
- Query by array index:
db.collection.find({ "scores.0" : { $gt : 80 } }) - Query by array element:
db.collection.find({ "scores" : { $gt : 80 } }) - Query by multiple array elements:
db.collection.find({ "scores" : { $all: [80, 85] } })
Query Embedded Documents
You can query embedded documents using dot notation:
db.collection.find({ "address.city" : "New York" })
This query returns all documents where the city field of the address embedded document is “New York”.
Type Check and Cast
You can check the type of a field using the $type operator and perform type casting using various BSON types:
db.collection.find({ "age" : { $type : "int" } })
This query returns all documents where the age field is of type int.