我有一个包含用户名,注册日期和赚取积分的数据集,我希望看到最接近的用户在平均水平附近(分别高25%和低25%)。
我尝试在第一阶段获得平均值:
db.people.aggregate([
{$group: {_id: null, "average_points" : { $avg : "$points" } }},
{ $project: {_id: 1,
"name": 1,
"points": 1,
"avg_25_h": {$multiply: [ "$average_points", 1.25 ] },
"avg_25_l": {$multiply: [ "$average_points", 0.75 ] } } }
] )
结果正确:
{
"_id" : null,
"avg_25_h" : 624350.9491925,
"avg_25_l" : 374610.5695155
}
但是,当我尝试匹配时,我只能获得在迷路时达到平均水平的用户:
db.people.aggregate([
{$group: {_id: null, "average_points" : { $avg : "$points" } }},
{ $project: {_id: null,
"name": 1,
"points": 1,
"avg_25_h": {$multiply: [ "$average_points", 1.25 ] },
"avg_25_l": {$multiply: [ "$average_points", 0.75 ] } } },
{$match: {"points": {$gte: "$avg_25_l", $lte:"avg_25_h"}}}
] )
我尝试使用实际的_id并淘汰了$ match阶段,但随后它计算出各个平均值并将其分别乘以0.75和1.25。
有人可以帮我吗?
谢谢!
######## UPDATE我终于明白了。这是我所做的:
db.people.aggregate([
{$group: {_id: null, "avg_points" : { $avg : "$points" } }},
{$lookup:
{
from: "people",
let: { points : "$points", avg_points: "$avg_points"},
pipeline: [
{$match:
{ $expr:
{$and:
[
{$gte: ["$points", {$multiply: ["$$avg_points", 0.75]}]},
{$lte: ["$points", {$multiply: ["$$avg_points", 1.25]}]}
]
}
}
},
{ $project: { padding: 0, signup: 0, current_date: 0 } },
{$limit: 20},
{$sort: {"points": -1}}
],
as: "UsersAroundTheAvg"
} },
{ $unwind: "$UsersAroundTheAvg" },
{ $project: { _id: 0, padding: 0, signup: 0, points: 0, current_date: 0, name: 0} }
])