MongoDB
This connector simplifies accessing MongoDB databases. It supports the following .NET drivers:
- MongoDB.Driver, which provides an
IMongoClient
.
The remainder of this page assumes you're familiar with the basic concepts of Steeltoe Connectors.
Usage
To use this connector:
- Create a MongoDB server instance or use a docker container.
- Add NuGet references to your project.
- Configure your connection string in
appsettings.json
. - Initialize the Steeltoe Connector at startup.
- Use the driver-specific connection/client instance.
Add NuGet References
To use this connector, add a NuGet reference to Steeltoe.Connectors
.
Also add a NuGet reference to one of the .NET drivers listed above, as you would if you were not using Steeltoe.
Configure connection string
The available connection string parameters for MongoDB are documented here.
The following example appsettings.json
uses the docker container from above:
{
"Steeltoe": {
"Client": {
"MongoDb": {
"Default": {
"ConnectionString": "mongodb://localhost:27017",
"Database": "TestCollection"
}
}
}
}
}
Notice this configuration file contains the database name, in addition to the connection string. This value is exposed
as MongoDbOptions.Database
.
Initialize Steeltoe Connector
Update your Program.cs
as below to initialize the Connector:
using Steeltoe.Connectors.MongoDb;
var builder = WebApplication.CreateBuilder(args);
builder.AddMongoDb();
Use IMongoClient
Start by defining a class that contains collection data:
using MongoDB.Bson;
public class SampleObject
{
public ObjectId Id { get; set; }
public string? Text { get; set; }
}
To obtain an IMongoClient
instance in your application, inject the Steeltoe factory in a controller or view:
using Microsoft.AspNetCore.Mvc;
using MongoDb.Data;
using MongoDB.Driver;
using Steeltoe.Connectors;
using Steeltoe.Connectors.MongoDb;
public class HomeController : Controller
{
public async Task<IActionResult> Index(
[FromServices] ConnectorFactory<MongoDbOptions, IMongoClient> connectorFactory)
{
var connector = connectorFactory.Get();
IMongoClient client = connector.GetConnection();
IMongoDatabase database = client.GetDatabase(connector.Options.Database);
IMongoCollection<SampleObject> collection = database.GetCollection<SampleObject>("SampleObjects");
List<SampleObject> sampleObjects = await collection.Find(obj => true).ToListAsync();
return View(sampleObjects);
}
}
A complete sample app that uses IMongoClient
is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/MongoDb.
Cloud Foundry
This Connector supports the following service brokers:
You can create and bind an instance to your application by using the Cloud Foundry CLI:
# Create MongoDB service
cf create-service csb-azure-mongodb small myMongoDbService
# Bind service to your app
cf bind-service myApp myMongoDbService
# Restage the app to pick up change
cf restage myApp
Kubernetes
This Connector supports the Service Binding Specification for Kubernetes. It can be used through the Bitnami Services Toolkit.
For details on how to use this, see the instructions at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/MongoDb#running-on-tanzu-application-platform-tap.