只返回Mongo中嵌套数组的内部元素

时间:2015-11-17 10:20:10

标签: mongodb meteor

我目前有一个包含这样的文档的集合

{
    "_id" : "sHXFGyTkZBYeZXcax",
    "name" : "Sunless Sunday",
    "description" : "blabla",
    "game_id" : "qPrZBahQLHQXabwuv",
    "date_checkin" : ISODate("2015-11-07T01:01:00.000Z"),
    "date_start" : ISODate("2015-11-12T00:04:00.000Z"),
    "date_end" : ISODate("2015-11-19T00:05:00.000Z"),
    "company_id" : 1,
    "featured" : 1,
    "premium" : 0,
    "type" : 0,
    "ongoing" : 1,
    "prizes" : [ 
        {
            "place" : 1,
            "amount" : 18
        }, 
        {
            "place" : 2,
            "amount" : 2
        }
    ],
    "createdAt" : ISODate("2015-11-05T22:34:01.494Z"),
    "modifiedAt" : ISODate("2015-11-05T22:34:01.494Z"),
    "owner" : "CLEopD9HRAeE9eiXW",
    "players" : [ 
        {
            "player_id" : "WdLK9aaRgdPnYsw8B",
            "status" : 2
        }, 
        {
            "player_id" : "vF6JEwMy9yaRtKuiG",
            "status" : 1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : -1
        }, 
        {
            "player_id" : "KD4s2E3AezhFcQDCd",
            "status" : 1
        }
    ],
    "rounds" : [ 
        {
            "roundNumber" : 1,
            "participants" : [],
            "matchesToWin" : 3,
            "matches" : [ 
                {
                    "matchNumber" : 1,
                    "party1" : "WdLK9aaRgdPnYsw8B"
                }
            ]
        }, 
        {
            "roundNumber" : 2,
            "participants" : [ 
                {
                    "player_id" : "WdLK9aaRgdPnYsw8B",
                    "status" : 2
                }, 
                {
                    "player_id" : "vF6JEwMy9yaRtKuiG",
                    "status" : 1
                }, 
                {
                    "player_id" : "KD4s2E3AezhFcQDCd",
                    "status" : 1
                }
            ],
            "matchesToWin" : 2,
            "matches" : [ 
                {
                    "matchNumber" : 1,
                    "players" : [ 
                        {
                            "party" : 1
                        }, 
                        {
                            "player_id" : "WdLK9aaRgdPnYsw8B",
                            "party" : 2
                        }
                    ],
                    "party1" : "freewin",
                    "party2" : "WdLK9aaRgdPnYsw8B",
                    "currentGame" : 1,
                    "winner" : "WdLK9aaRgdPnYsw8B",
                    "matchFinished" : ISODate("2015-11-16T16:25:37.712Z")
                }, 
                {
                    "matchNumber" : 2,
                    "players" : [ 
                        {
                            "player_id" : "vF6JEwMy9yaRtKuiG",
                            "party" : 1
                        }, 
                        {
                            "player_id" : "KD4s2E3AezhFcQDCd",
                            "party" : 2
                        }
                    ],
                    "party1" : "vF6JEwMy9yaRtKuiG",
                    "party2" : "KD4s2E3AezhFcQDCd",
                    "score1" : 0,
                    "score2" : 0,
                    "currentGame" : 1
                }
            ]
        }
    ]
}

现在我正在尝试查看某个玩家是否有活跃的匹配。所以这意味着玩家将在第一场比赛中成为party1或party2,目前没有获胜者。

我目前有这个查询

activeMatch = Tournaments.find({
        'ongoing': 1,
        'rounds.matches': {
          $elemMatch: {
            $or: [
              { 'party1': player[0]._id },
              { 'party2': player[0]._id },
            ],
            winner: { $exists: false }
          }
        }
      },
      { "rounds.matches.$": 1 }
    );

目前我有两个问题:

  • 我的查询返回匹配的回合,但不会过滤匹配的匹配。所以我实际上只有roundNumber = 2,但是从那一轮我得到每场比赛,因为我只对特定的比赛感兴趣。
  • 只有在我通过shell执行查询时才会进行过滤。当我在我的.jsx文件中使用它并将console.log结果发送给客户端时,我看到了所有轮次。

任何帮助?

0 个答案:

没有答案