Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add MongoDB example #1012

Merged
Merged
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
50 changes: 50 additions & 0 deletions docs/modules/mongodb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# MongoDB

[MongoDB](https://www.mongodb.com/what-is-mongodb) is a cross-platform document-oriented database. MongoDB's document model is simple for developers to use within their applications, while still providing all the complex capabilities of traditional relational databases.

The following example uses the following NuGet packages:

```console title="Install the NuGet dependencies"
dotnet add package Testcontainers.MongoDb
dotnet add package MongoDB.Driver
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.

Copy and paste the following code into a new `.cs` test file within an existing test project.

```csharp
using MongoDB.Driver;
using Testcontainers.MongoDb;
using Xunit;

namespace TestcontainersModules;

public class MongoDbContainerTest : IAsyncLifetime
{
private readonly MongoDbContainer mongoDbContainer =
new MongoDbBuilder()
.Build();

[Fact]
public async Task ReadFromMongoDbDatabase()
{
var client = new MongoClient(mongoDbContainer.GetConnectionString());

using var databases = await client.ListDatabasesAsync();

Assert.True(await databases.AnyAsync());
}

public Task InitializeAsync()
=> mongoDbContainer.StartAsync();

public Task DisposeAsync()
=> mongoDbContainer.DisposeAsync().AsTask();
}
```

To execute the tests, use the command `dotnet test` from a terminal.

[xunit]: https://xunit.net/