我正在使用 Studio3T 的 Intellishell。我希望修改我的“重复清理”脚本以在所有客户中运行,而不是需要为每个客户单独运行脚本。仅当该客户有相同的记录时,重复才被视为重复。客户之间的相同记录不被视为重复,不应清理。
db.assessment_dictionary.find({customer_id:"90uwjff0jjio89302"}).forEach(function(ad) {
db.assessments.aggregate(
[
{$match:{dictionary_id:ad._id}},
{"$group" : { "_id": {customer_id:"$customer_id",dictionary_id:"$dictionary_id",student_id:"$student_id",school_year:"$school_year",term_name:"$term_name",test_grade:"$test_grade",test_score:"$test_score"}, scores: {$addToSet: "$test_score"}, uniqueIds: { $addToSet: "$_id" }, "count": { "$sum": 1 } } },
{"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } }
],{allowDiskUse:true}
).forEach(function(doc) {
var ids = []
for (var i = 1; i<doc.uniqueIds.length; i++) {
ids.push(doc.uniqueIds[i]);
if (i%100==0) {
db.assessments.remove({_id:{$in:ids}})
ids = [];
}
// print ('Delete');
// } else {
// print ('Keep');
// }
}
db.assessments.remove({_id:{$in:ids}})
})
})