我的帖子中有一些阵容的一些相关属性:
players
,是一个数组,round
,是一个数字,team
,是一个字符串。我需要知道:
第二轮有多少相同的团队文档让玩家阵列为空,第一轮的任何相同团队文档都有一个非空的玩家阵列?
详细了解我的架构:
输入和输出示例:
在这个非常简单的案例中,只有2个团队的4个文件:
{team: "goats", players: ['3', '4', '3'], matchDate: {round: 1}}
{team: "eagles", players: ['3', '4', '3'], matchDate: {round: 1}}
{team: "goats", players: [], matchDate: {round: 2}}
{team: "eagles" ,players: [], matchDate: {round: 2}}
作为:
对于下一个修改示例,预期的查询结果将为1:
{team: "goats", players: ['3', '4', '3'], matchDate: {round: 1}}
{team: "eagles", players: ['3', '4', '3'], matchDate: {round: 1}}
{team: "goats", players: [], matchDate: {round: 2}}
{team: "eagles" ,players: ['2','8'], matchDate: {round: 2}}
如:
我如何检索该信息?
我刚开始尝试聚合,但这是我到目前为止所做的全部:
db.lineup.aggregate([
{$match: {"matchDate.round": {$lte: 2}}},
{$group: {_id: "$team", lineups: {$push: "$players"}}}
])