我正在使用mongodb缝合函数,我有两个集合,比如社区和帖子。当我在post集合中插入新文档时,我们需要在社区中增加+1 a summary.postCount。我在post集合中将状态更新为已删除,我们需要在社区集合中递减-1 \f25 a summary.postCount -1。我像这样写这个函数。
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}
发布于 2020-02-27 02:22:36
这里肯定有某种bug。我可以通过做{$inc: {"x": Number(1)}}来绕过它。这仍然会将其更改为int64,但这至少比double更接近。
https://stackoverflow.com/questions/60162613
复制相似问题