【问题标题】:Convert filed value in integer type in mongodb during aggregate [duplicate]在聚合期间转换mongodb中整数类型的字段值[重复]
【发布时间】:2020-03-01 18:44:44
【问题描述】:

如何在mongodb聚合期间将字符串转换为int类型?

{ $lookup:{ from: 'circle', localField:'circle_id', foreignField: 'circle_id', as: 'circleDetail' } },
  • localField: 'circle_id' -> int 类型

  • foreignField: 'circle_id' -> 字符串类型

我想将外部字段转换为int类型。

【问题讨论】:

  • 您将“circle_id”存储在哪个变量中?
  • 我没有得到你的要求。顺便说一句,我加入了 2 个基于 circle_id 的集合
  • 在您的查找中使用 foreignField: +'circle_id' 这会将您的字符串转换为数字。
  • 我做了但没有弄错这是我在 circle_id $lookup 参数 'foreignField: nan.0' 之前添加 + 后得到的错误,必须是字符串,是双精度类型
  • 为什么它们的类型不同?

标签: node.js string mongodb int aggregate


【解决方案1】:

您可以使用$convert$toInt 运算符。这适用于 4.0 版。

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多