Mongo Aggregation 删除一个 id 的所有记录,只保留最旧的记录

the*_*own 5 aggregate-functions mongodb mongodb-query aggregation-framework

有一个带有值的集合

_id:ObjectId('......')
ton_id :ObjectId('abcd')
value:587900
date:2019-12-13T07:09:40.075+00:00


_id:ObjectId('......')
ton_id :ObjectId('abcd')
value:50540
date:2018-1-13T07:09:40.075+00:00

_id:ObjectId('......')
ton_id :ObjectId('abcd1')
value:55400
date:2019-5-13T07:09:40.075+00:00


_id:ObjectId('......')
ton_id :ObjectId('abcd1')
value:22500
date:2018-12-13T07:09:40.075+00:00


Run Code Online (Sandbox Code Playgroud)

对于 ton_idsabcdabcd1删除除最旧记录之外的所有记录。

所需的输出

_id:ObjectId('......')
ton_id :ObjectId('abcd')
value:50540
date:2018-1-13T07:09:40.075+00:00



_id:ObjectId('......')
ton_id :ObjectId('abcd1')
value:22500
date:2018-12-13T07:09:40.075+00:00

Run Code Online (Sandbox Code Playgroud)

use*_*814 1

类似的东西应该有效。使用聚合来选择要保留的 ID,然后进行批量更新以删除其他 ID。

var bulk = db.getCollection(colname).initializeUnorderedBulkOp();

db.getCollection(colname).aggregate([
    {$match:{"ton_id":{"$in":[abcd, abcd1]}}},
    {$sort:{"date":1}},
    {$group:{
        "_id":"$ton_id", 
        "keep_id":{"$first":"$_id"}
    }},
    {$project:{"_id":0, "ton_id":"$_id", "keep_id":1}}
]).forEach(function(doc){ 
    bulk.find({"_id":{"$ne":doc.keep_id},"ton_id":doc.ton_id}).remove();
}); 
bulk.execute(); 
Run Code Online (Sandbox Code Playgroud)