Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class AggregationPipelineLiveTest : IAsyncLifetime
{
private readonly MongoDbContainer _mongoDbContainer =
new MongoDbBuilder().Build();
new MongoDbBuilder("mongo:8.0").Build();

[Fact]
public async Task GivenIHaveUsersAndRolesCollectionsInMongoDB_WhenICallTheGetUserModelsMethod_ThenItMergesTheTwoCollectionsIntoOneResult()
Expand All @@ -30,7 +30,7 @@ public async Task GivenIHaveUsersAndRolesCollectionsInMongoDB_WhenICallTheGetUse
await MongoHelper.AddSeedData(database);

//Act
var actualResult = await sut.GetAllUsers();
var actualResult = await sut.GetAllStudentsAsync();

//Assert
Assert.NotNull(actualResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.6.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
<PackageReference Include="Testcontainers.MongoDb" Version="4.14.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.6.0" />
<PackageReference Include="MongoDB.Driver" Version="3.10.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="4.14.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.ComponentModel.DataAnnotations;

namespace JoinCollectionsAggregationPipeline.Models;

public class Course
{
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = string.Empty;
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = string.Empty;

[Required]
[BsonElement("Name")]
[BsonElement("Name")]
public string Name { get; set; } = string.Empty;

[Required]
[BsonElement("Code")]
public string Code { get; set; } = string.Empty;
public string Code { get; set; } = string.Empty;

public override bool Equals(object? obj)
{
if (obj is not Course course) return false;
if (obj is not Course course) return false;
return Name == course.Name
&& Code == course.Code;
}

public override int GetHashCode()
{
return HashCode.Combine(Id, Name, Code);
return HashCode.Combine(Name, Code);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using System.ComponentModel.DataAnnotations;

namespace JoinCollectionsAggregationPipeline.Models;

public class Student {

public class Student
{
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = string.Empty;
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = string.Empty;

[Required]
[BsonElement("FirstName")]
public string FirstName { get; set; } = string.Empty;
[BsonElement("FirstName")]
public string FirstName { get; set; } = string.Empty;

[Required]
[BsonElement("LastName")]
[BsonElement("LastName")]
public string LastName { get; set; } = string.Empty;

[Required]
[BsonElement("Major")]
public string Major { get; set; } = string.Empty;

[BsonElement("StudentCourses")]
public List<Course> StudentCourses { get; set; } = new();
[BsonElement("StudentCourses")]
public List<Course> StudentCourses { get; set; } = [];

public override bool Equals(object? obj)
{
if (obj is not Student student) return false;
return FirstName == student.FirstName &&
LastName == student.LastName
&& StudentCourses.All(course => course
.Equals(student.StudentCourses.ElementAt(StudentCourses.IndexOf(course))));
public override bool Equals(object? obj)
{
if (obj is not Student student) return false;
return FirstName == student.FirstName
&& LastName == student.LastName
&& StudentCourses.SequenceEqual(student.StudentCourses);
}

public override int GetHashCode()
{
return HashCode.Combine(Id, FirstName, LastName, Major, StudentCourses);
var hash = new HashCode();
hash.Add(FirstName);
hash.Add(LastName);

foreach (var course in StudentCourses)
hash.Add(course);

return hash.ToHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Testcontainers.MongoDb;
using JoinCollectionsAggregationPipeline.Models;

await using var mongoDbContainer = new MongoDbBuilder().Build();
await using var mongoDbContainer = new MongoDbBuilder("mongo:8.0").Build();

await mongoDbContainer.StartAsync();
var mongoClient = new MongoClient(mongoDbContainer.GetConnectionString());
Expand All @@ -14,10 +14,10 @@
var database = mongoClient.GetDatabase(DatabaseConfiguration.DatabaseName);
await MongoHelper.AddSeedData(database);

var users = await repository.GetAllUsers();
foreach (var user in users)
var students = await repository.GetAllStudentsAsync();
foreach (var student in students)
{
Console.WriteLine(user.ToJson());
Console.WriteLine(student.ToJson());
}

await mongoDbContainer.StopAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public StudentRepository(MongoClient client)
_studentCollection = database.GetCollection<Student>("Students");
}

public async Task<List<Student>> GetAllUsers()
public async Task<List<Student>> GetAllStudentsAsync()
{
//Empty Pipeline
var studentAggregationPipeline = _studentCollection.Aggregate();
Expand Down
Loading