gpt4 book ai didi

mongodb shell $inc 将 int 转换为 double

转载 作者:行者123 更新时间:2023-12-04 01:55:10 24 4
gpt4 key购买 nike

使用 1 衬垫通过 mongodb 中的 $inc 通过 mongo shell 更新许多记录,我发现我正在触摸的整数字段被转换为 double 。我知道 shell 使用 javascript,这就是原因。我的问题:有没有一种方法可以通过以某种方式传入 NumberInt 来继续使用 1-liner 来执行此更新,或者我是否需要其他东西?鉴于:

{ _id: 1, sku: "abc123", quantity: 10, metrics: { orders: 2, ratings: 3.5 } }

然后我尝试使用 1-liner。

db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: -2, "metrics.orders": 1 } }
)

数量应该是 8 而不是 8.0,metrics.orders 应该是 3 而不是 3.0。

最佳答案

要保留原始整数数据类型,您需要将 $inc 值作为 NumberInt 传递:

db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: NumberInt(-2), "metrics.orders": NumberInt(1) } }
)

关于mongodb shell $inc 将 int 转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com