Introduction to MongoDB $toString Operator
In MongoDB, the $toString
operator is used to convert any type of data to a string type. The $toString
operator can be used in the $project
stage of the aggregation pipeline, as well as in the $match
and $lookup
stages of queries.
Syntax
The syntax for $toString
is as follows:
{ $toString: <expression> }
Here, <expression>
is any MongoDB expression, which can be a document field, a constant, or another expression.
Use Cases
The $toString
operator can be used to convert any type of data to a string type. For example, in the aggregation pipeline, when it is necessary to convert numerical types, date types, Boolean types, etc. to a string type for processing, the $toString
operator can be used.
Examples
Here are two examples of using the $toString
operator:
Example 1
Assume we have the following document collection:
{ "_id": 1, "name": "Alice", "age": 20, "gender": "F" }
{ "_id": 2, "name": "Bob", "age": 25, "gender": "M" }
{ "_id": 3, "name": "Charlie", "age": 30, "gender": "M" }
Now, we need to concatenate the age
field and the gender
field into a string type field and perform string matching. We can use the $toString
operator to convert the age
and gender
fields to a string type, then use the $concat
operator to concatenate them into a string type field, and finally use the $match
operator to perform string matching. The code for this example is as follows:
db.collection.aggregate([
{
$project: {
name: 1,
age: 1,
gender: 1,
age_gender: {
$concat: [{ $toString: "$age" }, "-", "$gender"]
}
}
},
{
$match: {
age_gender: /^2/
}
}
])
In the example above, we use the $project
operator to concatenate the age
and gender
fields into an age_gender
field and convert the age
and gender
fields to a string type. Then we use the $match
operator to perform string matching and filter out documents where the age_gender
field starts with 2.
Example 2
Assume we have the following document collection:
{ "_id": 1, "price": 10.5 }
{ "_id": 2, "price": 20.5 }
{ "_id": 3, "price": 30.5 }
Now, we need to convert the price
field to a string type and add the $
unit to the string type price
field. We can use the $toString
operator to convert the price
field to a string type, then use the $concat
operator to concatenate them into a string type field. The code for this example is as follows:
db.collection.aggregate([
{
$project: {
price: 1,
price_str: {
$concat: ["$", { $toString: "$price" }]
}
}
}
])
In the example above, we use the $project
operator to concatenate the price
field into a string type field and add the $
unit to the string type price
field.
In addition to string concatenation and regular expression matching, $toString
can also be used to convert other data types to string type. For example, to convert an integer type to a string type, the sample code is as follows:
db.students.aggregate([
{
$project: {
score: 90,
scoreString: { $toString: "$score" }
}
}
])
After running the above aggregation pipeline, the output result is as follows:
{ "_id": ObjectId("617a980f3c3c3d3b05647a85"), "score": 90, "scoreString": "90" }
In the above example, we use the $toString
operator to convert the integer type score
field to a string type and store it in the scoreString
field.
Conclusion
The $toString
operator can convert other data types to string type and can be used in scenarios such as string concatenation and regular expression matching. When using the $toString
operator, it is important to consider the compatibility of the data types to ensure that the resulting conversion meets expectations.