如何在 mongo 展开后获取之前的值
How to get a previous value after a mongo unwind
是否可以在 $unwind
之后访问以前的文档,然后 $group
在新的(已清理)object 中将它们一起访问?
使用这个 Mongo aggregate
: mongoplayground.net/p/IsM-h9DnhjJ 我想得到页面内所有结果 stats
和 scores
的平均值,然后网站所有页面统计数据的平均值。
Collections
我有三个 collections 都是相关的。 Site > Page > Result(页面结果)
// sites
{
_id: "si-site-a",
teamId: "te-team-1",
name: "Site a"
}
// pages
{
_id: "pa-page-a",
teamId: "te-team-1",
siteId: "si-site-a",
name: "Page A",
status: "ok"
},
{
_id: "pa-page-b",
teamId: "te-team-1",
siteId: "si-site-a",
name: "Page B",
status: "ok"
}
// results
{
_id: "re-result-1",
pageId: "pa-page-a",
siteId: "si-site-a",
results: {
scores: {
performance: 0.75,
accessibility: 0.98,
"best-practices": 0.93,
seo: 1,
pwa: 0.36,
ecoscore: 0.94
},
stats: {
transferSize: 876995,
co2grams: 0.11,
pageLoadTime: 1722.284
}
},
region: "gb1",
timestamp: ISODate("2022-02-05T16:38:35.454Z"),
status: "ok"
}
期望的输出
{
_id: 'a',
name: 'Site a',
pages: [
{
_id: 'aa',
name: 'Page a',
status: 'ok',
resultAverages: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
},
],
pageAverages: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
comparedTo: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
percentageChange: {
ecoscore: 0,
transferSize: 0,
co2grams: -0.19,
pageLoadTime: 0.24,
},
},
您实际上在使用 sub-pipeline 的正确轨道上。您可以先在 sub-pipeline 中计算结果中的平均值。然后将它们存储在个人 result
中。在 sub-pipeline 之后,执行另一个 $avg
来计算之前的内部结果的平均值,以获得 pages
.
的平均值
db.sites.aggregate([
{
$lookup: {
from: "pages",
localField: "_id",
foreignField: "siteId",
as: "pages",
pipeline: [
{
$lookup: {
from: "result",
localField: "_id",
foreignField: "pageId",
as: "results",
pipeline: [
{
$match: {
timestamp: {
$gte: ISODate("2022-01-01"),
$lte: ISODate("2022-12-01")
},
status: "ok"
}
}
]
}
},
{
$project: {
_id: 1,
name: 1,
status: 1,
resultAverages: {
ecoscore: {
$avg: "$results.results.stats.ecosore"
},
transferSize: {
$avg: "$results.results.stats.transferSize"
},
co2grams: {
$avg: "$results.results.stats.co2grams"
},
pageLoadTime: {
$avg: "$results.results.stats.pageLoadTime"
}
}
}
}
]
}
},
{
"$addFields": {
pageAverages: {
ecoscore: {
$avg: "$pages.resultAverages.ecosore"
},
transferSize: {
$avg: "$pages.resultAverages.transferSize"
},
co2grams: {
$avg: "$pages.resultAverages.co2grams"
},
pageLoadTime: {
$avg: "$pages.resultAverages.pageLoadTime"
}
}
}
}
])
这里是Mongo playground供您参考。
是否可以在 $unwind
之后访问以前的文档,然后 $group
在新的(已清理)object 中将它们一起访问?
使用这个 Mongo aggregate
: mongoplayground.net/p/IsM-h9DnhjJ 我想得到页面内所有结果 stats
和 scores
的平均值,然后网站所有页面统计数据的平均值。
Collections
我有三个 collections 都是相关的。 Site > Page > Result(页面结果)
// sites
{
_id: "si-site-a",
teamId: "te-team-1",
name: "Site a"
}
// pages
{
_id: "pa-page-a",
teamId: "te-team-1",
siteId: "si-site-a",
name: "Page A",
status: "ok"
},
{
_id: "pa-page-b",
teamId: "te-team-1",
siteId: "si-site-a",
name: "Page B",
status: "ok"
}
// results
{
_id: "re-result-1",
pageId: "pa-page-a",
siteId: "si-site-a",
results: {
scores: {
performance: 0.75,
accessibility: 0.98,
"best-practices": 0.93,
seo: 1,
pwa: 0.36,
ecoscore: 0.94
},
stats: {
transferSize: 876995,
co2grams: 0.11,
pageLoadTime: 1722.284
}
},
region: "gb1",
timestamp: ISODate("2022-02-05T16:38:35.454Z"),
status: "ok"
}
期望的输出
{
_id: 'a',
name: 'Site a',
pages: [
{
_id: 'aa',
name: 'Page a',
status: 'ok',
resultAverages: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
},
],
pageAverages: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
comparedTo: {
ecoscore: 0.89,
transferSize: 556081,
co2grams: 0.19,
pageLoadTime: 1011.7159999999999,
},
percentageChange: {
ecoscore: 0,
transferSize: 0,
co2grams: -0.19,
pageLoadTime: 0.24,
},
},
您实际上在使用 sub-pipeline 的正确轨道上。您可以先在 sub-pipeline 中计算结果中的平均值。然后将它们存储在个人 result
中。在 sub-pipeline 之后,执行另一个 $avg
来计算之前的内部结果的平均值,以获得 pages
.
db.sites.aggregate([
{
$lookup: {
from: "pages",
localField: "_id",
foreignField: "siteId",
as: "pages",
pipeline: [
{
$lookup: {
from: "result",
localField: "_id",
foreignField: "pageId",
as: "results",
pipeline: [
{
$match: {
timestamp: {
$gte: ISODate("2022-01-01"),
$lte: ISODate("2022-12-01")
},
status: "ok"
}
}
]
}
},
{
$project: {
_id: 1,
name: 1,
status: 1,
resultAverages: {
ecoscore: {
$avg: "$results.results.stats.ecosore"
},
transferSize: {
$avg: "$results.results.stats.transferSize"
},
co2grams: {
$avg: "$results.results.stats.co2grams"
},
pageLoadTime: {
$avg: "$results.results.stats.pageLoadTime"
}
}
}
}
]
}
},
{
"$addFields": {
pageAverages: {
ecoscore: {
$avg: "$pages.resultAverages.ecosore"
},
transferSize: {
$avg: "$pages.resultAverages.transferSize"
},
co2grams: {
$avg: "$pages.resultAverages.co2grams"
},
pageLoadTime: {
$avg: "$pages.resultAverages.pageLoadTime"
}
}
}
}
])
这里是Mongo playground供您参考。