首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB缝合-当使用update递减时,如何保持数据类型为整型?

MongoDB缝合-当使用update递减时,如何保持数据类型为整型?
EN

Stack Overflow用户
提问于 2020-02-11 13:37:58
回答 1查看 37关注 0票数 0

我正在使用mongodb缝合函数,我有两个集合,比如社区和帖子。当我在post集合中插入新文档时,我们需要在社区中增加+1 a summary.postCount。我在post集合中将状态更新为已删除,我们需要在社区集合中递减-1 \f25 a summary.postCount -1。我像这样写这个函数。

代码语言:javascript
复制
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}

EN

回答 1

Stack Overflow用户

发布于 2020-02-27 02:22:36

这里肯定有某种bug。我可以通过做{$inc: {"x": Number(1)}}来绕过它。这仍然会将其更改为int64,但这至少比double更接近。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60162613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档