mongodb - Intellishell 重复文档清理
问题描述
我正在使用 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}})
})
})
解决方案
推荐阅读
- vue.js - Vue JS - mouseleave 上的清除间隔
- python - 为什么我的双循环打印出一个奇怪的值
- c# - Asp.Net MVC 4 模型绑定和剔除绑定以选择元素
- java - 如何使用 Envoy 作为 Java Web 服务的负载平衡器?
- mysql - 如何在MYSQL用户定义函数中通过另一个字符串变量引用一个局部变量名?
- python-3.x - 大约 1234 次编辑后,edit_message 协程上的 ClientResponseError
- tensorflow - 是否可以将 tf.contrib.quantize.create_training_graph 与 Keras 模型一起使用?
- python - Python 3:没有名为“AudioSignal”的模块
- java - 尽管资源处理程序映射,spring boot 仍为 index.html 提供服务
- vba - 从文档开头查找和替换,但将光标返回到上一个位置