Node.js MongoDB Stitch-当数据类型随更新递增时,如何将其保持为int(而不更改为double)?

Node.js MongoDB Stitch-当数据类型随更新递增时,如何将其保持为int(而不更改为double)?,node.js,mongodb,mongodb-query,mongodb-stitch,Node.js,Mongodb,Mongodb Query,Mongodb Stitch,我在mongodb中使用stitch。在stitch中,我编写了一个nodejs函数,用于递增和递减 if(changeEvent.fullDocument.type == 'article' || changeEvent.fullDocument.type == 'poll' ) { context.functions.execute("notifyTopic", "article-created", changeEvent.fullDocument); var communities

我在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 NUMBERRINT,也是这样

 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”的工作事件,但没有任何用处

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

检查以下内容:

  • 您这样做是对的,
    {$inc:{“summary.commentCount”:1}
    it 将任何
    Int32
    字段转换为
    Double
  • 所以一开始你应该试试
    {$inc:{“summary.commentCount”:numberprint(1)}
  • 现在是 使用步骤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
    ,您可能需要找到一种方法来使用各自的值更新所有文档,可以使用或读取、操作数据和更新。

    考虑以下文档:
    {a:numberprint(5),b:50}
    b
    的字段值,如果默认情况下a
    double
    。如果从Mongo Shell执行此操作:
    db.collection.updateOne({},{$inc:{a:numberprint(1),b:numberprint(1)})
    ,则
    a
    b
    的值都增加1。
    a
    的数据类型(整数)和
    b
    (双精度)将保持不变。@prasad文档如下摘要:{postCount:1,lastActivity:date}数据类型是Int,但当我执行上述代码时,数据类型从Int转换为double。你能检查一个特定的文档并查看更新前后的情况吗?你是如何检查类型是否为
    Int
    ?@prasad我在mongodb atlas中进行了检查。在stitch中,我编写了function@prasad当我输入N时,该函数中不接受numberprintumberInt,递增/递减不在该函数中发生。如何写入递增/递减。在上面的解决方案中,刚转换为整数。但我只需要在递增后用整数进行后计数/decrements@RameshReddy:在
    Int32
    字段上,如果使用
    numberprint(1)
    那么它将始终是
    Int32
    。由于您在不知不觉中使用了
    1
    而不是上面给出的值,因此需要将其转换回
    Int32
    &此后您可以使用
    numberrint(1)
    。我使用numberrint(1)但是numberprint not working事件也不会增加值。@Rameshredy:你能直接在DB上测试它吗?请告诉我。谢谢兄弟,我会提出新的查询。