Introduction to MongoDB $toObjectId Operator
$toObjectId
is a built-in operator in MongoDB used to convert a string to an ObjectId. It can be used for projection, filtering, and other operations in aggregation pipelines.
Syntax
The syntax for the $toObjectId
operator is as follows:
{ $toObjectId: <expression> }
Where <expression>
is a string expression, which can be any expression, but is usually a field name.
Use Cases
In MongoDB, the _id
field is typically stored as an ObjectId type, and in MongoDB query operations, documents can be searched by the _id
field. Therefore, in some scenarios, we need to convert a string field to an ObjectId type for querying or sorting.
Examples
Here are two examples of the $toObjectId
operator:
Example 1
Suppose we have a users
collection, where each document contains an _id
field storing a UUID as a string type, and we want to convert the _id
field to an ObjectId type for querying or sorting.
Create the users
collection and insert two documents:
db.users.insertMany([
{ _id: "5cfc7b8e37ab39164c1d33a1", name: "Alice" },
{ _id: "5cfc7b8e37ab39164c1d33a2", name: "Bob" }
])
Use the $toObjectId
operator to convert the _id
field to an ObjectId
type:
db.users.aggregate([
{
$project: {
_id: { $toObjectId: "$_id" },
name: 1
}
}
])
In the above code, we use the $project
operator to convert the _id
field to an ObjectId type.
The output is as follows:
{ "_id" : ObjectId("5cfc7b8e37ab39164c1d33a1"), "name" : "Alice" }
{ "_id" : ObjectId("5cfc7b8e37ab39164c1d33a2"), "name" : "Bob" }
Example 2
Suppose we have an orders
collection, where each document contains an orderDate
field storing a date as a string type, and we want to sort the data by date.
Create the orders
collection and insert two documents:
db.orders.insertMany([
{ orderDate: "2022-03-03", amount: 100 },
{ orderDate: "2022-03-01", amount: 200 }
])
Use the $toObjectId
operator to convert the orderDate
field to an ObjectId type:
db.orders.aggregate([
{
$project: {
orderDate: { $toDate: "$orderDate" },
amount: 1
}
},
{
$sort: {
orderDate: 1
}
}
])
In the above code, we use the $project
operator to convert the orderDate
field to a date type, then use the $sort
operator to sort the data by orderDate
in ascending order, and finally use the $project
operator again to convert orderDate
back to a string type.
It should be noted that the $dateToString
operator is used in the $project
operator to convert the date type orderDate
field to a string type and specify the output format. In this example, we used the format of “YYYY-MM-DD”. If other output formats are needed, please refer to the relevant content in the MongoDB official documentation.
Conclusion
Through this article, we have learned about the syntax, usage scenarios, examples, and conclusions of the $toObjectId
operator in MongoDB. The $toObjectId
operator can convert the specified string into an ObjectId type, which is commonly used in scenarios where ObjectId needs to be used in a query statement. In the examples, we demonstrated the application of the $toObjectId
operator in queries and provided two complete examples to help readers better understand. In practical applications, the $toObjectId
operator is a very useful tool when querying operations need to use ObjectId types.