python - 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 但也满足查找逻辑的最新文档
我需要某种聚合(我认为)
解决方案
您可以尝试以下查询:
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-游乐场
推荐阅读
- django - 是否可以对 Django 平面页面进行 dockerize 处理?
- c# - 使用C#从另一个arrayList中减去一个arrayList
- javascript - 上下文 API 问题 - 对象未定义
- proxy - 如何在 aiohttp 中使用 auth 代理向 https 网站发出请求?
- c# - C# UITypeEditor Dropdown 不适用于 Click 事件
- javascript - 如何在 javascript 中读取 geojson 文件并获取属性的最小值或最大值?
- php - 在 laravel 中转发数据以获取电子邮件内容
- vue.js - TypeError:无法读取未定义的属性“宽度” - Nuxt JS 和画布
- javascript - 谷歌应用脚本语法错误
- java - 数字未添加到现有数组的后面