MongoDB聚合运算符:$toObjectId

MongoDB聚合运算符:$toObjectId


$toObjectId聚合运算符将指定的值转换为ObjectId。如果值无法被转换为ObjectId,则报错。

语法

{
   $toObjectId: <expression>
}

$toObjectId接受任何有效的表达式。

$toObjectId$convert表达式的简写形式:

{ $convert: { input: <expression>, to: "objectId" } }

使用

下表列出了可转换为ObjectId的类型:

输入类型 规则
String 返回长度为 24 的十六进制字符串的 ObjectId。如果字符串值不是长度为 24 的十六进制字符串,则无法转换。

下表列出了一些转换为ObjectId的示例:

示例 结果
{$toObjectId: "5ab9cbfa31c2ab715d42129e"} ObjectId("5ab9cbfa31c2ab715d42129e")
{$toObjectId: "5ab9cbfa31c2ab715d42129"} Error

举例

使用下面的脚本创建orders集合:

db.orders.insertMany( [
   { _id: "5ab9cbe531c2ab715d42129a", item: "apple", qty: 10 },
   { _id: ObjectId("5ab9d0b831c2ab715d4212a8"), item: "pie", qty: 5 },
   { _id: ObjectId("5ab9d2d331c2ab715d4212b3"), item: "ice cream", qty: 20 },
   { _id: "5ab9e16431c2ab715d4212b4", item: "almonds", qty: 50 },
] )

下面的聚合操将_id转换为ObjectId,并进行排序:

// 定义转换阶段,将_id值转换为ObjectId并添加到文档

idConversionStage = {
   $addFields: {
      convertedId: { $toObjectId: "$_id" }
   }
};

// 定义排序阶段,根据convertedId进行排序


sortStage = {
   $sort: { "convertedId": -1 }
};


db.orders.aggregate( [
   idConversionStage,
   sortStage
] )

执行的结果为:

{
  _id: '5ab9e16431c2ab715d4212b4',
  item: 'almonds',
  qty: 50,
  convertedId: ObjectId("5ab9e16431c2ab715d4212b4")
},
{
  _id: ObjectId("5ab9d2d331c2ab715d4212b3"),
  item: 'ice cream',
  qty: 20,
  convertedId: ObjectId("5ab9d2d331c2ab715d4212b3")
},
{
  _id: ObjectId("5ab9d0b831c2ab715d4212a8"),
  item: 'pie',
  qty: 5,
  convertedId: ObjectId("5ab9d0b831c2ab715d4212a8")
},
{
  _id: '5ab9cbe531c2ab715d42129a',
  item: 'apple',
  qty: 10,
  convertedId: ObjectId("5ab9cbe531c2ab715d42129a")
}

相关推荐

  1. MongoDB聚合运算符:$toObjectId

    2024-05-10 09:34:08       33 阅读
  2. MongoDB聚合运算符:$add

    2024-05-10 09:34:08       55 阅读
  3. MongoDB聚合运算符:$arrayToObject

    2024-05-10 09:34:08       52 阅读
  4. MongoDB聚合运算符;$dateToParts

    2024-05-10 09:34:08       52 阅读
  5. MongoDB聚合运算符:$dayOfWeek

    2024-05-10 09:34:08       53 阅读
  6. MongoDB聚合运算符:$dayOfMonth

    2024-05-10 09:34:08       50 阅读
  7. MongoDB聚合运算符;$dateToString

    2024-05-10 09:34:08       47 阅读
  8. MongoDB聚合运算符:$dayOfYear

    2024-05-10 09:34:08       48 阅读
  9. MongoDB聚合运算符:$denseRank

    2024-05-10 09:34:08       42 阅读
  10. MongoDB聚合运算符:$dateTrunc

    2024-05-10 09:34:08       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 09:34:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 09:34:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 09:34:08       82 阅读
  4. Python语言-面向对象

    2024-05-10 09:34:08       91 阅读

热门阅读

  1. React 学习-4

    2024-05-10 09:34:08       36 阅读
  2. iOS-SSL固定证书

    2024-05-10 09:34:08       34 阅读
  3. 快速了解Vuex

    2024-05-10 09:34:08       28 阅读
  4. 按键精灵、autojs、冰狐智能辅助到底该如何选择?

    2024-05-10 09:34:08       135 阅读
  5. Vue3:视图渲染

    2024-05-10 09:34:08       27 阅读
  6. VMware 的三种网络模式

    2024-05-10 09:34:08       27 阅读
  7. flutter 在onError函数中不推荐使用“runZoned

    2024-05-10 09:34:08       35 阅读
  8. rust学习(openssl rsa加解密文件)

    2024-05-10 09:34:08       33 阅读
  9. C++ 746. 使用最小花费爬楼梯

    2024-05-10 09:34:08       27 阅读
  10. hbase建表预分区的2种方法

    2024-05-10 09:34:08       25 阅读