首页 > 解决方案 > MongoDB - 聚合以提取唯一文档

问题描述

我是 MongoDB 新手,我的文档如下所示:

_id:5e58f340862df1a7b4e62b35
Date:1996-01-04T00:00:00.000+00:00
abd: 22
bvh: 21
ThingID:166
ThingValue:1
_id:5e58hf4862df1a7b4e62b35
Date:2000-01-04T00:00:00.000+00:00
abd: 22
bvh: 21
ThingID:166
ThingValue:1

这两个代表相同的东西,但一个是 1996 年的,另一个是 2000 年的 - 对我来说最好的方法是:

SELECT (ThingID, ThingValue) where abd = abd, bvh = bvh AND Date is latest

我试过这个:

mongo.db[collection].find({'$and': [{'abd': {'$eq': abd}},{'bvh': {'$eq': bvh}}]},
                             {"ThingID": 1,"ThingValue": 1, "_id": 0})
                    .sort([("Date", -1)])
                    .distinct('ThingID'))

如果它们只是我想要返回的 1 个 ThingID,则此方法有效 - 但是,可能有任意数量的满足此逻辑的不同 ThingID,我将不得不返回所有它们..

IE:

_id:5e58cd90862df1a7b4e62b35
Date:1996-01-04T00:00:00.000+00:00
abd: 22
bvh: 21
ThingID:166
ThingValue:1
_id:5e58cd67562df1a7b4e62b35
Date:2000-01-04T00:00:00.000+00:00
abd: 22
bvh: 21
ThingID:166
ThingValue:1
_id:5e76cd90862df1a7b4e62b35
Date:2000-01-04T00:00:00.000+00:00
abd: 22
bvh: 21
ThingID:167
ThingValue:1

我需要返回文档(5e58cd90862df1a7b4e62b35、e58cd90862df1a7b4e62b35),因为这是具有唯一 ThingID 但也满足查找逻辑的最新文档

我需要某种聚合(我认为)

标签: pythonpython-3.xmongodb

解决方案


您可以尝试以下查询:

db.collection.aggregate([
    /** match docs on criteria to lessesn dataset size */
    {
        $match: {
            abd: 22,
            bvh: 21
        }
    },
    /** sort on Date field to get latest docs up on all documents left after match stage */
    {
        $sort: {
            Date: -1
        }
    },
    /** group on ThingID & push first found document to field named data */
    {
        $group: {
            _id: "$ThingID",
            data: {
                $first: "$$ROOT"
            }
        }
    },
    /** So make data field as root, So data field will be new document */
    {
        $replaceRoot: {
            newRoot: "$data"
        }
    },
    /** Project field only what we need, By default _id will be present so we need to pass _id: 0 to ignore it */
    {
        $project: {
            ThingID: 1,
            ThingValue: 1,
            _id: 0
        }
    }
])

测试: MongoDB-游乐场


推荐阅读