MongoDB Stitch - 如何在随更新增加时将数据类型保持为 int(不更改为 double)? [英] MongoDB Stitch - How to keep datatype to int (without changing to double) when incremented with update?

查看:64
本文介绍了MongoDB Stitch - 如何在随更新增加时将数据类型保持为 int(不更改为 double)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mongodb 中使用针迹.在缝中,我编写了一个 nodejs 函数用于递增和递减目的.

I am using stitch in mongodb. In stitch, i write one nodejs function for increment and decrements purpose.

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也是这样的.

Now i have executed this function, the summary.postCount value converted from int to Double, I am usnig NumberInt also like this.

 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",但没有用.

this was not working event i have mentioned "summary.$.postCount" also, but there is no use.

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

I need the Increment/decrements values and datatype integer only. Please help me with the solution. Thanks in advance.

推荐答案

检查这个:

  1. 你这样做是对的 {$inc: {"summary.commentCount":1} 它会将任何 Int32 字段转换为 Double.
  2. 所以一开始你应该尝试过{$inc: {"summary.commentCount":NumberInt(1)}.
  3. 现在是已经使用步骤 2 转换为 double 以进行进一步查询是无用的.您需要将您的字段设置回原始数据类型 &然后继续您的进一步操作.
  1. You're right when you do this {$inc: {"summary.commentCount":1} it would convert any Int32 field to Double.
  2. So at first you should've tried {$inc: {"summary.commentCount":NumberInt(1)}.
  3. Now it's already converted to double for further queries using step 2 is useless. You need to set your field back to original data type & then go ahead with your further operations.

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

On MongoDB v > 4.2, you can run an aggregation pipeline in update() to update all docs from double to int on field postCount.

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

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

If you're using MongoDB v < 4.2 you might need to find a way to update all docs with their respective values, maybe read, manipulate data & update using .bulkWrite() or .updateMany().

这篇关于MongoDB Stitch - 如何在随更新增加时将数据类型保持为 int(不更改为 double)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆