【问题标题】:MongoDB Stitch - How to keep datatype to int (without changing to double) when incremented with update?MongoDB Stitch - 如何在更新时将数据类型保持为 int(不更改为 double)?
【发布时间】:2020-02-10 15:34:44
【问题描述】:

我在mongodb中使用stitch。在stitch中,我编写了一个nodejs函数用于递增和递减目的。

if(changeEvent.fullDocument.type == 'article' || changeEvent.fullDocument.type == 'poll' ) {
  context.functions.execute("notifyTopic", "article-created", changeEvent.fullDocument);
  var communities = context.services.get("mongodb-atlas").db("utilo").collection("communities");
  communities.updateOne(
    {_id:changeEvent.fullDocument.community},
    {$inc: {"summary.postCount":NumberInt(1)}, $currentDate: {"summary.lastActivity":true} }
  )
} else if (changeEvent.fullDocument.type == 'comment') {
  context.functions.execute("notifyTopic", "comment-created", changeEvent.fullDocument);
  var posts = context.services.get("mongodb-atlas").db("utilo").collection("posts");
  posts.updateOne(
    {_id:changeEvent.fullDocument.root},
    {$inc: {"summary.commentCount":1}, $currentDate: {"summary.lastActivity":true} }
  )

现在我已经执行了这个函数,summary.postCount值从int转换为Double,我usnig NumberInt也是这样的。

 posts.updateOne(
    {_id:changeEvent.fullDocument.root},
    {$inc: {"summary.commentCount":NumberInt(1)}, $currentDate: {"summary.lastActivity":true} }
  )

communities.updateOne(
    {_id:changeEvent.fullDocument.community},
    {$inc: {"summary.postCount":NumberInt(-1)}, $currentDate: {"summary.lastActivity":true} }
  )

这不是工作事件,我也提到了“summary.$.postCount”,但没有用。

我只需要增量/减量值和数据类型整数。请帮我解决问题。提前致谢。

【问题讨论】:

  • 考虑这份文件:{ a: NumberInt(5), b: 50 }b 的字段值,如果默认为 double。如果您从 Mongo Shell:db.collection.updateOne( { }, { $inc: { a: NumberInt(1), b: NumberInt(1) } } ) 执行此操作,ab 的值都增加 1。a(整数)和 b(双精度)的数据类型将保持不变。
  • @prasad 类似这样的文档摘要:{postCount:1, lastActivity: date} 数据类型是 Int,但是当我执行上述代码时,数据类型从 int 转换为 double
  • 您可以检查特定文档并查看更新前后的内容吗?你是如何检查类型是否为int的?
  • @prasad 我已经检查了 mongodb 地图集。在缝合中,我编写了函数
  • @prasad NumberInt 在该函数中不被接受,当我放入 NumberInt 时,该函数中不会发生递增/递减

标签: node.js mongodb mongodb-query mongodb-stitch


【解决方案1】:

检查一下:

  1. 当你这样做时你是对的{$inc: {"summary.commentCount":1}它 会将任何Int32 字段转换为Double
  2. 所以一开始你应该尝试过 {$inc: {"summary.commentCount":NumberInt(1)}
  3. 现在是 已经使用步骤 2 转换为 double 以进行进一步查询是无用的。您需要将您的字段设置回原始数据类型,然后继续您的进一步操作。

MongoDB v > 4.2 上,您可以在update() 中运行聚合管道,以将字段postCount 上的所有文档从double 更新为int。

db.collection.update(
    {},
    [
        { $set: { postCount: { $toInt: '$postCount' } } }
    ], false, true // 1) false is {upsert : false}, 2) is {multi : true}
)

如果您使用的是MongoDB v < 4.2,您可能需要找到一种方法来使用它们各自的值更新所有文档,也许可以使用.bulkWrite().updateMany() 读取、操作数据和更新。

【讨论】:

  • 如何写增量/减量。在上述解决方案中,只是转换为整数。但我只需要整数的 postCount,在递增/递减之后
  • @RameshReddy :在Int32 字段上,如果您使用NumberInt(1),那么它将始终为Int32.. 因为您在不知不觉中只使用了1 而不是上面的,因为它已转换加倍,所以你需要转换回Int32 & 之后你可以使用NumberInt(1)
  • 我正在使用 NumberInt(1) 但 NumberInt not working 事件也不会增加值。
  • @RameshReddy : 你能直接在数据库上测试它并告诉我。
  • 谢谢兄弟,我会提出新的查询。
猜你喜欢
  • 1970-01-01
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2020-08-29
  • 2015-11-23
  • 2022-01-15
  • 2014-06-18
相关资源
最近更新 更多