Hey guys, I've been struggling for days trying to eliminate all the duplicates from my database. I attempted a solution I came across on this Link, but encountered an error message stating that forEach is not a function. I'm puzzled as to why it works for others and not for me, even though the code is very similar. Here's the code I've been experimenting with:
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 running this code within a stitch function