如何在流星中使用rawCollection进行聚合?

问题描述:

我需要在集合中使用bulk操作,以删除database中某些条件的重复项。我试图使用rawCollection()但我真的不知道如何。如何在流星中使用rawCollection进行聚合?

这是我需要cron执行每隔x小时

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

    if(count !== 0) { 
    bulk.execute(); 
    } 
} 

的代码,但它会产生一个错误:Cannot call method 'forEach' of undefined

那么,应该怎么办?

好了,经过一些研究,我发现类似的问题,在这里我就是这样做,以使这项工作:

var aggregate = Meteor.wrapAsync(collection.aggregate, collection); 

然后

aggregate(parameters).forEach(...);