mongo 数据库平均差异
mongo DB average difference
我正在为一些作业中的 MongoDB 查询而苦苦挣扎。我是 MongoDb 的新手,到目前为止只了解基础知识。这是问题:
Suppose that we have full-time results data since 2001-08-01
for
a variety of football competitions in a mongo database collection
results which has the following fields kick_off_date, competition_id,
home_team_id, away_team_id, home_score, away_score.
If the competition_id of the English Premiership is 1
and the team_id
of Everton is 5
, write the mongo query that would return the average
difference in goals scored and goals conceded by Everton since 2005-01-01
in >English Premiership games where they were playing away.
到目前为止我有这个:
db.results.find( { competition_id,: 1, away_team_id: 5, kick_off_date: { $gte : new ISODate("2005-1-1T00:00:00Z") } } )
我认为这是自 2005-01-01
以来埃弗顿在英超联赛中的相关数据。但是除了使用电子表格之外,我不知道如何返回进球数和失球数的平均差异。
谁能给我指出正确的方向?
首先要注意:像你一样,我是 mongoDB 的新手,所以尽管下面给出的答案似乎有效,但可能有更简洁/更高效的方法来实现相同的结果。
这是我构建的示例数据集:
/* 0 */
{
"_id" : ObjectId("54d62ce0e11e084bc1366195"),
"kick_off_date" : ISODate("2005-01-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 1,
"away_team_id" : 5,
"home_score" : 1,
"away_score" : 3
}
/* 1 */
{
"_id" : ObjectId("54d62cece11e084bc1366196"),
"kick_off_date" : ISODate("2005-02-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 2,
"away_team_id" : 5,
"home_score" : 3,
"away_score" : 1
}
/* 2 */
{
"_id" : ObjectId("54d62cfde11e084bc1366197"),
"kick_off_date" : ISODate("2005-03-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 3,
"away_team_id" : 5,
"home_score" : 5,
"away_score" : 0
}
/* 3 */
{
"_id" : ObjectId("54d62d0ce11e084bc1366198"),
"kick_off_date" : ISODate("2005-04-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 4,
"away_team_id" : 5,
"home_score" : 0,
"away_score" : 5
}
从这个数据可以看出,净胜球是:
"_id" : ObjectId("54d62ce0e11e084bc1366195"),
"difference" : -2
"_id" : ObjectId("54d62cece11e084bc1366196"),
"difference" : 2
"_id" : ObjectId("54d62cfde11e084bc1366197"),
"difference" : 5
"_id" : ObjectId("54d62d0ce11e084bc1366198"),
"difference" : -5
因为里面的负差,把这些值加起来就是0
,这是没有用的。因此查询必须考虑到这一点并将负数视为正数以给出总数 14
。平均差异将是:(14/4) = 3.5
下面是执行所有这些操作的聚合查询:
db.full_time_results.aggregate(
{$match:
{
competition_id: 1,
away_team_id: 5,
kick_off_date: { $gte : ISODate("2005-01-01T00:00:00Z") }
}
},
{$project:
{
away_team_id:1,
difference: {$subtract:["$home_score","$away_score"]}
}
},
{$group:
{
_id:"$away_team_id",
avg_difference: {$avg:
{$cond:
{
if: { $lt: [ "$difference", 0 ] },
then: {$multiply:["$difference", -1]},
else: "$difference"
}
}
}
}
}
)
最后,结果:
{
"_id" : 5,
"avg_difference" : 3.5
}
我正在为一些作业中的 MongoDB 查询而苦苦挣扎。我是 MongoDb 的新手,到目前为止只了解基础知识。这是问题:
Suppose that we have full-time results data since
2001-08-01
for a variety of football competitions in a mongo database collection results which has the following fields kick_off_date, competition_id, home_team_id, away_team_id, home_score, away_score.If the competition_id of the English Premiership is
1
and the team_id of Everton is5
, write the mongo query that would return the average difference in goals scored and goals conceded by Everton since2005-01-01
in >English Premiership games where they were playing away.
到目前为止我有这个:
db.results.find( { competition_id,: 1, away_team_id: 5, kick_off_date: { $gte : new ISODate("2005-1-1T00:00:00Z") } } )
我认为这是自 2005-01-01
以来埃弗顿在英超联赛中的相关数据。但是除了使用电子表格之外,我不知道如何返回进球数和失球数的平均差异。
谁能给我指出正确的方向?
首先要注意:像你一样,我是 mongoDB 的新手,所以尽管下面给出的答案似乎有效,但可能有更简洁/更高效的方法来实现相同的结果。
这是我构建的示例数据集:
/* 0 */
{
"_id" : ObjectId("54d62ce0e11e084bc1366195"),
"kick_off_date" : ISODate("2005-01-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 1,
"away_team_id" : 5,
"home_score" : 1,
"away_score" : 3
}
/* 1 */
{
"_id" : ObjectId("54d62cece11e084bc1366196"),
"kick_off_date" : ISODate("2005-02-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 2,
"away_team_id" : 5,
"home_score" : 3,
"away_score" : 1
}
/* 2 */
{
"_id" : ObjectId("54d62cfde11e084bc1366197"),
"kick_off_date" : ISODate("2005-03-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 3,
"away_team_id" : 5,
"home_score" : 5,
"away_score" : 0
}
/* 3 */
{
"_id" : ObjectId("54d62d0ce11e084bc1366198"),
"kick_off_date" : ISODate("2005-04-16T10:35:54.985Z"),
"competition_id" : 1,
"home_team_id" : 4,
"away_team_id" : 5,
"home_score" : 0,
"away_score" : 5
}
从这个数据可以看出,净胜球是:
"_id" : ObjectId("54d62ce0e11e084bc1366195"),
"difference" : -2
"_id" : ObjectId("54d62cece11e084bc1366196"),
"difference" : 2
"_id" : ObjectId("54d62cfde11e084bc1366197"),
"difference" : 5
"_id" : ObjectId("54d62d0ce11e084bc1366198"),
"difference" : -5
因为里面的负差,把这些值加起来就是0
,这是没有用的。因此查询必须考虑到这一点并将负数视为正数以给出总数 14
。平均差异将是:(14/4) = 3.5
下面是执行所有这些操作的聚合查询:
db.full_time_results.aggregate(
{$match:
{
competition_id: 1,
away_team_id: 5,
kick_off_date: { $gte : ISODate("2005-01-01T00:00:00Z") }
}
},
{$project:
{
away_team_id:1,
difference: {$subtract:["$home_score","$away_score"]}
}
},
{$group:
{
_id:"$away_team_id",
avg_difference: {$avg:
{$cond:
{
if: { $lt: [ "$difference", 0 ] },
then: {$multiply:["$difference", -1]},
else: "$difference"
}
}
}
}
}
)
最后,结果:
{
"_id" : 5,
"avg_difference" : 3.5
}