我需要对聚合使用批量
操作,以便删除数据库
中具有特定条件的重复项。我尝试使用 rawCollection()
但我真的不知道如何使用。
这是我需要每 x 小时使用 cron
执行一次的代码
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();
}
}
但它会产生错误:无法调用未定义的方法“forEach”
那我该怎么办?
最佳答案
好吧,经过一番研究后,我发现了类似的问题,这就是我为完成这项工作所做的工作:
var aggregate = Meteor.wrapAsync(collection.aggregate, collection);
然后
aggregate(parameters).forEach(...);
关于meteor - 如何在 meteor 中使用 rawCollection 进行聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917503/