Bulk operations


#1

How do I make bulk operations inside meteor on server?
I tried collection.rawCollection() but it doesn’t work.
Here’s the code I need to execute with cron every x hours:

let bulk  = collection.rawCollection().initializeUnorderedBulkOp(),
count = 0;

collection.aggregate([
  { '$sort': { 'difficultyrating': -1 }},
  { '$group': { '_id': '$beatmapset_id', 'ids': { '$push': '$_id' }, 'count': { '$sum': 1 }}},
  { '$match': { 'count': { '$gt': 1 } }},
]).forEach((doc) => {
  doc.ids.shift();
  bulk.find({'_id': { '$in': doc.ids }}).remove();
  count++;
  if (count === 100) {
      bulk.execute();
      bulk = collection.rawCollection().initializeUnorderedBulkOp();
  }
});
if (count !== 0) {
  bulk.execute();
}

I’m using meteorhacks:aggregate package for that.
It produces an error:

W20151003-02:28:41.040(3)? (STDERR) C:\Users\1\AppData\Local\.meteor\packages\npm-mongo\1.4.39_1\npm\node_modules\mongodb\lib\mongodb\connection\base.js:246
W20151003-02:28:41.040(3)? (STDERR)
W20151003-02:28:41.041(3)? (STDERR)               ^
W20151003-02:28:41.040(3)? (STDERR)         throw message;
W20151003-02:28:41.041(3)? (STDERR) TypeError: undefined is not a function

Also if I run aggregate with collection.rawCollection().aggregate(…) it produces this error:

 Cannot call method 'forEach' of undefined

If I run this code inside mongo shell it works fine.
So what should I do?