Is it possible to aggregate through realm app?

Hi everyone i’m trying that should remove all the duplicates but it returns uncaught promise rejection: TypeError, am I doin something wrong or it is just not possible? Here’s the code

        exports = function(payload, response) {
            const mongodb = context.services.get("mongodb-atlas");
            //var oggetti=EJSON.parse(payload.body.text())
            //var inserimentoDB = mongodb.db("tesi").collection("cookies").insertMany(oggetti)
          const pipeline=[
              { $group: {
                  _id: { 'Valore': '$Valore' },
                  dups: { '$addToSet': '$_id' },
                  count: { '$sum': 1 }
              }},
              { '$match': { count: { '$gt': 1 } }}
            ]
            mongodb.db("tesi").collection("cookies").aggregate(pipeline).toArray().then(docs => docs.forEach(function (doc){
              doc.dups.shift();
              mongodb.db("tesi").collection("cookies").dups.delete({
                _id:{$in: doc.dups}
              })
            }))
        

did you make it work?