【问题标题】:MongoDB Stitch - How to keep datatype to int when decremented with update?MongoDB Stitch - 如何在更新减少时将数据类型保持为 int?
【发布时间】:2020-02-27 02:22:35
【问题描述】:

我正在使用 mongodb 缝合功能,我有两个集合,如社区和帖子。当我在帖子集合中插入一个新文档时,我们需要在社区集合中增加一个 summary.postCount。当我在帖子集合中将状态更新为已删除时,我们需要在社区集合中减少 -1 一个 summary.postCount。我是这样写函数的。

if(changeEvent.operationType == 'insert') {
context.functions.execute('addEventBySystem', 
  {
    community: changeEvent.fullDocument.community,
    channel: changeEvent.fullDocument.channel,
    origin: changeEvent.fullDocument.lastUpdatedBy,
    target: "",
    type: 'created',
    status: 'completed'
  });
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":1}, $currentDate: {"summary.lastActivity":true} }
  )
} else if(changeEvent.operationType == 'update') {
if(changeEvent.fullDocument.status == "deleted" && changeEvent.fullDocument.type == 'article' || 
changeEvent.fullDocument.type == 'poll') {
  var communities = context.services.get("mongodb-atlas").db("utilo").collection("communities");
  communities.updateOne(
    {_id:changeEvent.fullDocument.community},
    {$inc: {"summary.postCount":-1}, $currentDate: {"summary.lastActivity":true} }
  )
}

现在,在递减时,summary.postCount 数据类型从 int32 更改为 double。我也尝试过 NumberInt 但没有用。如何仅在递增/递减后保持数据类型 int?

注意:在社区摘要中归档如下摘要:{postCount:1,lastActivity:date}

【问题讨论】:

    标签: javascript node.js mongodb-stitch


    【解决方案1】:

    这里肯定发生了某种错误。我可以通过{$inc: {"x": Number(1)}} 来解决它。这仍然会将其更改为 int64,但至少比双精度更接近。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-08
      • 2020-08-29
      • 1970-01-01
      • 2022-01-16
      • 2020-10-22
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多