I have a mongo database with two collections: Tournament and Player.
A tournament contains, apart from simple attributes like _id and Name, a list of Match objects.
I have a query, in which I want to find the player with the most matches won. Before, I also had a collection Match in which all matches were contained. However I wanted to use the nested documents feature of MongoDB so I put those matches in Tournament instead.
The query before (one that worked):
var players = _matchCollection
.Unwind<Match, Match>(m => m.WinnerId)
.Group(m => m.WinnerId, g => new
{
Id = g.First().WinnerId,
MatchesWon = g.Count()
}).ToList();
Now, my attempts so far (gives no error, but the players sequence contains no objects):
var players = _tournamentCollection.Aggregate().Unwind<Tournament,Match>(tour => tour.Matches)
.Unwind<Match, Match>(m => m.WinnerId)
.Group(m => m.WinnerId, g => new
{
Id = g.First().WinnerId,
MatchesWon = g.Count()
}).ToList();
So my question is: how can I make such a query work on the nested array of matches?
EDIT: Added the relevant classes.
Tournament:
public class Tournament
{
public string? Id { get; set; }
public string? Name { get; set; }
public string? Surface { get; set; }
public int? DrawSize { get; set; }
public string? Level { get; set; }
public string? Date { get; set; }
public List<Match> Matches { get; set; } = new();
}
Match:
public class Match
{
public string? Id { get; set; }
public string? MatchNum { get; set; }
public string? WinnerId { get; set; }
public string? LoserId { get; set; }
public string? Score { get; set; }
public string? BestOf { get; set; }
public string? Round { get; set; }
public string? TourneyId { get; set; }
}

Tournamentto the question will be helpful.