我有以下查询:我有重复的文档,但我需要删除它们,但保留mongodb中具有最大字段的文档。重复的字段是"financialAccount“我设法进行了查询,但在某些情况下,它会删除我不想要的文档。你能帮我拿一下这个吗?谢谢。
我需要保留包含较大字段的文档,并删除其他重复的字段。
发布于 2020-08-11 08:09:29
最后,我不得不使用highDate字段作为过滤器来删除记录。只保留具有最旧的"highDate“字段的记录。
bulk = db.financialAccount.initializeUnorderedBulkOp();
count = 0;
db.financialAccount.aggregate([
{ '$sort': { 'createDate': 1 }},
{ $match : { financialAccount: {"$in":[
'548541000'
]}}},
{ '$group': { '_id': '$financialAccount', 'ids': { '$push': '$_id' }, 'count': { '$sum': 1 },'financialAccount':{'$first': "$financialAccount"}}},
{ '$match': { 'count': { '$gt': 1 }}}
]).forEach(function(doc) {
doc.ids.shift();
bulk.find({'_id': { '$in': doc.ids }}).remove();
count++;
if(count === 100) {
bulk.execute();
bulk = db.financialAccount.initializeUnorderedBulkOp();
}
})
if(count !== 0) {
bulk.execute();
}https://stackoverflow.com/questions/63309499
复制相似问题