Hi everyone i've been trying for days now to remove from my db all the duplicates that are in it. I've tried this solution that I found at this Link or at this one which is almost the same but unfortunately it returns the error that forEach is not a function. I don't understand why for them works and not for me even if the code is almost the same. Here's the code that i've tried so far
exports = function(payload, response) {
const mongodb = context.services.get("mongodb-atlas");
var obj=EJSON.parse(payload.body.text())
var inserimentoDB = mongodb.db("test").collection("test0").insertMany(obj)
var duplicates = [];
mongodb.db("test").collection("test0").aggregate([
{ "$group": {
"_id": { "Val": "$Val" },
"dups": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
{ "$match": { "count": { "$gt": 1 } }}
]).toArray().forEach(function(doc) {
doc.dups.shift();
mongodb.db("test").collection("test0").remove({ "_id": {"$in": doc.dups }});
});
}
I'm executing this code in a stitch function