Skip to content

Commit 3eb87af

Browse files
Merge pull request #35 from yongshun950824/Dec_2023
Add Solution_089
2 parents 193ca0c + 42c6513 commit 3eb87af

6 files changed

Lines changed: 188 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"_id": {
4+
"$oid": "658e52ec7b7649b28fbbf5f1"
5+
},
6+
"Name": "League One"
7+
}
8+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"_id": {
4+
"$oid": "658e529b7b7649b28fbbf5ee"
5+
},
6+
"FirstName": "Player",
7+
"LastName": "One",
8+
"Handicap": 0
9+
},
10+
{
11+
"_id": {
12+
"$oid": "658e52d87b7649b28fbbf5ef"
13+
},
14+
"FirstName": "Player",
15+
"LastName": "Two",
16+
"Handicap": 0
17+
}
18+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"_id": {
4+
"$oid": "658e53097b7649b28fbbf5f3"
5+
},
6+
"Number": 1,
7+
"LeagueId": {
8+
"$oid": "658e52ec7b7649b28fbbf5f1"
9+
},
10+
"Player1Id": {
11+
"$oid": "658e529b7b7649b28fbbf5ee"
12+
},
13+
"Player2Id": {
14+
"$oid": "658e52d87b7649b28fbbf5ef"
15+
}
16+
}
17+
]

MongoDBConsoleApp/MongoDBConsoleApp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
<Compile Include="Solutions\Solution_086.cs" />
267267
<Compile Include="Solutions\Solution_087.cs" />
268268
<Compile Include="Solutions\Solution_088.cs" />
269+
<Compile Include="Solutions\Solution_089.cs" />
269270
</ItemGroup>
270271
<ItemGroup>
271272
<None Include="App.config" />
@@ -345,6 +346,9 @@
345346
<None Include="Data\Solution_085.json" />
346347
<None Include="Data\Solution_086_people.json" />
347348
<None Include="Data\Solution_087_Class.json" />
349+
<None Include="Data\Solution_089_Leagues.json" />
350+
<None Include="Data\Solution_089_Players.json" />
351+
<None Include="Data\Solution_089_Teams.json" />
348352
<None Include="libmongocrypt.dylib" />
349353
<None Include="libmongocrypt.so" />
350354
<None Include="packages.config" />
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Written Solutions for StackOverflow questions.
9696
| [Solution_086][162] | [MongoDB C# - How to do sorting and pagination with an array of objects on a record][163] |
9797
| [Solution_087][164] | [MongoDB - Count number of object in array][165] |
9898
| [Solution_088][166] | [Backward Compatible Mongo fields using C# and .NET][167] |
99+
| [Solution_089][168] | [How to Aggregate MongoDB Collections using C# Classes][169] |
99100

100101

101102
[1]: https://github.com/yongshun950824/MongoDBConsoleApp/blob/master/MongoDBConsoleApp/Solutions/Solution_001.cs
@@ -342,4 +343,7 @@ Written Solutions for StackOverflow questions.
342343
[165]: https://stackoverflow.com/questions/79433036/mongodb-count-number-of-object-in-array/79433069#79433069
343344

344345
[166]: https://github.com/yongshun950824/MongoDBConsoleApp/blob/master/MongoDBConsoleApp/Solutions/Solution_088.cs
345-
[167]: https://stackoverflow.com/questions/79568837/backward-compatible-mongo-fields-using-c-sharp-and-net/79568869#79568869
346+
[167]: https://stackoverflow.com/questions/79568837/backward-compatible-mongo-fields-using-c-sharp-and-net/79568869#79568869
347+
348+
[168]: https://github.com/yongshun950824/MongoDBConsoleApp/blob/master/MongoDBConsoleApp/Solutions/Solution_089.cs
349+
[169]: https://stackoverflow.com/questions/77728716/how-to-aggregate-mongodb-collections-using-c-sharp-classes/77730452#77730452

0 commit comments

Comments
 (0)