Вы можете попробовать следующий запрос:
db.collection.aggregate([
/** Group on 'goalsFor' & 'goalsAgainst' for similars & push teams to 'teams' array,
* Will be one or more (if there are any similar teams with same 'goalsFor' & 'goalsAgainst' then that doc will have 2 or more elements/teams in teams array) */
{
$group: {
_id: { goalsFor: "$goalsFor", goalsAgainst: "$goalsAgainst" },
teams: { $push: "$team" }
}
},
/** Filter docs where there are multiple teams in 'teams' array */
{ $match: { $expr: { $gte: [{ $size: "$teams" }, 2] } } }
]);
Тест: MongoDB-Playground
Обновление до ответа:
Для MongoDB версии 2.6.10
, попробуйте следующий запрос:
db.collection.aggregate([
{
$group: {
_id: {
goalsFor: "$goalsFor",
goalsAgainst: "$goalsAgainst"
},
teams: {
$push: "$team"
}
}
},
{
$project: {
teams: 1,
_id: 0,
goalsFor: "$_id.goalsFor",
goalsAgainst: "$_id.goalsAgainst",
teamsSize: {
$size: "$teams"
}
}
},
{
$match: {
teamsSize: {
$gte: 2
}
}
},
{
$project: {
teamsSize: 0
}
}
])
Тест: MongoDB-Playground