I am using mongodb stitch function, I have two collections like communities and posts. when i inserted a new document in post collection, we need to increment +1 a summary.postCount in communities collection.when i updated the status to deleted in post collection, we need to decrement -1 a summary.postCount in communities collection. I write the function like this.
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} }
)
}
Now, at the time of decrement, summary.postCount datatype changed from int32 to double. I tried to NumberInt also but no use. How to keep datatype int only after increment/decrement?
Note: In Communities summary filed like this summary:{postCount:1,lastActivity:date}