|
| 1 | +using MongoDB.Bson; |
| 2 | +using MongoDB.Bson.Serialization.Attributes; |
| 3 | +using MongoDB.Driver; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace MongoDBConsoleApp.Solutions |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// <a href="https://stackoverflow.com/questions/77728716/how-to-aggregate-mongodb-collections-using-c-sharp-classes/77730452#77730452"> |
| 11 | + /// Question. |
| 12 | + /// </a> |
| 13 | + /// </summary> |
| 14 | + internal class Solution_089 : ISolution |
| 15 | + { |
| 16 | + public void Run(IMongoClient _client) |
| 17 | + { |
| 18 | + RunAsync(_client).GetAwaiter().GetResult(); |
| 19 | + } |
| 20 | + |
| 21 | + public async Task RunAsync(IMongoClient _client) |
| 22 | + { |
| 23 | + IMongoDatabase Database = _client.GetDatabase("demo"); |
| 24 | + |
| 25 | + IMongoCollection<TeamData> teamCollection = Database.GetCollection<TeamData>("Teams"); |
| 26 | + IMongoCollection<League> leagues = Database.GetCollection<League>("Leagues"); |
| 27 | + IMongoCollection<Player> players = Database.GetCollection<Player>("Players"); |
| 28 | + |
| 29 | + var data = (from team in teamCollection.AsQueryable() |
| 30 | + join league in leagues.AsQueryable() on team.LeagueId equals league.Id |
| 31 | + join player1 in players.AsQueryable() on team.Player1Id equals player1.Id |
| 32 | + join player2 in players.AsQueryable() on team.Player2Id equals player2.Id |
| 33 | + select new Team |
| 34 | + { |
| 35 | + Id = team.Id, |
| 36 | + League = league, |
| 37 | + Number = team.Number, |
| 38 | + Player1 = player1, |
| 39 | + Player2 = player2, |
| 40 | + }).ToList(); |
| 41 | + |
| 42 | + data = await teamCollection.Aggregate<Team>(new BsonDocument[] |
| 43 | + { |
| 44 | + new BsonDocument("$lookup", |
| 45 | + new BsonDocument |
| 46 | + { |
| 47 | + { "from", "Leagues" }, |
| 48 | + { "localField", "LeagueId" }, |
| 49 | + { "foreignField", "_id" }, |
| 50 | + { "as", "Leagues" } |
| 51 | + }), |
| 52 | + new BsonDocument("$lookup", |
| 53 | + new BsonDocument |
| 54 | + { |
| 55 | + { "from", "Players" }, |
| 56 | + { "localField", "Player1Id" }, |
| 57 | + { "foreignField", "_id" }, |
| 58 | + { "as", "PlayerOnes" } |
| 59 | + }), |
| 60 | + new BsonDocument("$lookup", |
| 61 | + new BsonDocument |
| 62 | + { |
| 63 | + { "from", "Players" }, |
| 64 | + { "localField", "Player2Id" }, |
| 65 | + { "foreignField", "_id" }, |
| 66 | + { "as", "PlayerTwos" } |
| 67 | + }), |
| 68 | + new BsonDocument("$project", |
| 69 | + new BsonDocument |
| 70 | + { |
| 71 | + { "_id", 1 }, |
| 72 | + { "Number", 1 }, |
| 73 | + { "League", |
| 74 | + new BsonDocument("$first", "$Leagues") }, |
| 75 | + { "Player1", |
| 76 | + new BsonDocument("$first", "$PlayerOnes") }, |
| 77 | + { "Player2", |
| 78 | + new BsonDocument("$first", "$PlayerTwos") } |
| 79 | + }) |
| 80 | + }).ToListAsync(); |
| 81 | + |
| 82 | + Helpers.PrintFormattedJson(data); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public class Player |
| 87 | + { |
| 88 | + [BsonId, BsonRepresentation(BsonType.ObjectId)] |
| 89 | + public string Id { get; set; } |
| 90 | + |
| 91 | + public string FirstName { get; set; } |
| 92 | + |
| 93 | + public string LastName { get; set; } |
| 94 | + |
| 95 | + public int Handicap { get; set; } |
| 96 | + } |
| 97 | + |
| 98 | + public class League |
| 99 | + { |
| 100 | + [BsonId, BsonRepresentation(BsonType.ObjectId)] |
| 101 | + public string Id { get; set; } |
| 102 | + |
| 103 | + public string Name { get; set; } |
| 104 | + } |
| 105 | + |
| 106 | + public class Team |
| 107 | + { |
| 108 | + [BsonId, BsonRepresentation(BsonType.ObjectId)] |
| 109 | + public string Id { get; set; } |
| 110 | + |
| 111 | + public int Number { get; set; } |
| 112 | + |
| 113 | + public League League { get; set; } |
| 114 | + |
| 115 | + public Player Player1 { get; set; } |
| 116 | + |
| 117 | + public Player Player2 { get; set; } |
| 118 | + } |
| 119 | + |
| 120 | + public class TeamData |
| 121 | + { |
| 122 | + [BsonId, BsonRepresentation(BsonType.ObjectId)] |
| 123 | + public string Id { get; set; } |
| 124 | + |
| 125 | + public int Number { get; set; } |
| 126 | + |
| 127 | + [BsonRepresentation(BsonType.ObjectId)] |
| 128 | + public string LeagueId { get; set; } |
| 129 | + |
| 130 | + [BsonRepresentation(BsonType.ObjectId)] |
| 131 | + public string Player1Id { get; set; } |
| 132 | + |
| 133 | + [BsonRepresentation(BsonType.ObjectId)] |
| 134 | + public string Player2Id { get; set; } |
| 135 | + } |
| 136 | +} |
0 commit comments