对引用id相等的文档字段求和



我有以下结构的文档:

{
"_id" : ObjectId("60c42bc26296623b0056515a"),
"team" : [ 
ObjectId("60c42acf6296623b00565153")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565155")
],
"goals_favor" : 5,
"goals_against" : 2,
"goals_difference" : 3,
"matches_played" : 1,
"won_matches" : 1,
"tied_matches" : 0,
"lost_matches" : 0,
"points" : 3
}
{
"_id" : ObjectId("60c42bc26296623b0056515b"),
"team" : [ 
ObjectId("60c42acf6296623b00565154")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565155")
],
"goals_favor" : 2,
"goals_against" : 5,
"goals_difference" : -3,
"matches_played" : 1,
"won_matches" : 0,
"tied_matches" : 0,
"lost_matches" : 1,
"points" : 0
}
{
"_id" : ObjectId("60c4eb5aaa5d6523c83f59c0"),
"team" : [ 
ObjectId("60c42acf6296623b00565153")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565156")
],
"goals_favor" : 5,
"goals_against" : 2,
"goals_difference" : 3,
"matches_played" : 1,
"won_matches" : 1,
"tied_matches" : 0,
"lost_matches" : 0,
"points" : 3
}
{
"_id" : ObjectId("60c4eb5aaa5d6523c83f59c1"),
"team" : [ 
ObjectId("60c42acf6296623b00565154")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565156")
],
"goals_favor" : 2,
"goals_against" : 5,
"goals_difference" : -3,
"matches_played" : 1,
"won_matches" : 0,
"tied_matches" : 0,
"lost_matches" : 1,
"points" : 0
}
{
"_id" : ObjectId("60c4eb99aa5d6523c83f59c4"),
"team" : [ 
ObjectId("60c42acf6296623b00565153")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565157")
],
"goals_favor" : 5,
"goals_against" : 2,
"goals_difference" : 3,
"matches_played" : 1,
"won_matches" : 1,
"tied_matches" : 0,
"lost_matches" : 0,
"points" : 3
}
{
"_id" : ObjectId("60c4eb99aa5d6523c83f59c5"),
"team" : [ 
ObjectId("60c42acf6296623b00565154")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565157")
],
"goals_favor" : 2,
"goals_against" : 5,
"goals_difference" : -3,
"matches_played" : 1,
"won_matches" : 0,
"tied_matches" : 0,
"lost_matches" : 1,
"points" : 0
}

我正在尝试获取所有具有goals_favor, goals_against, goals_difference, match_played, won_matches, tied_matches, lost_matches, and points求和字段的文档,其中team字段中的引用id相同,请注意,团队字段'60c42acf6296623b00565153'中有三个文档具有以下id,其余三个文档的id为'60c42acf6296623b00565154',因此我想要类似于以下的结构:

{
"_id" : ObjectId("60c42bc26296623b0056515a"),
"team" : [ 
ObjectId("60c42acf6296623b00565153")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565155")
],
"goals_favor" : 15,
"goals_against" : 6,
"goals_difference" : 9,
"matches_played" : 3,
"won_matches" : 3,
"tied_matches" : 0,
"lost_matches" : 0,
"points" : 9
}
{
"_id" : ObjectId("60c42bc26296623b0056515b"),
"team" : [ 
ObjectId("60c42acf6296623b00565154")
],
"matchday" : [ 
ObjectId("60c42b386296623b00565155")
],
"goals_favor" : 6,
"goals_against" : 15,
"goals_difference" : -9,
"matches_played" : 3,
"won_matches" : 0,
"tied_matches" : 0,
"lost_matches" : 3,
"points" : 0
}

我已经尝试过了,但无法遵循:

const ids = ['60c42acf6296623b00565153', '60c42acf6296623b00565154']
TeamsDetails.aggregate([
{ $match: { "team": { $in: ids } } },
{ $group: { _id: "$_id", total: { $sum: "$points" } } },
]).exec((error, find) => {
//Do something
})

谢谢。

您需要使用$unwind通过team来解构数组和组

db.collection.aggregate([
// match stage
{
$unwind: "$team"
},
{
"$group": {
"_id": "$team",
"points": {
"$sum": "$points"
},
goals_favor: {
$sum: "$goals_favor"
}
}
}
])

正在工作的Mongo游乐场

最新更新