Tingle.Extensions.MongoDB 4.12.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tingle.Extensions.MongoDB --version 4.12.0                
NuGet\Install-Package Tingle.Extensions.MongoDB -Version 4.12.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tingle.Extensions.MongoDB" Version="4.12.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tingle.Extensions.MongoDB --version 4.12.0                
#r "nuget: Tingle.Extensions.MongoDB, 4.12.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tingle.Extensions.MongoDB as a Cake Addin
#addin nuget:?package=Tingle.Extensions.MongoDB&version=4.12.0

// Install Tingle.Extensions.MongoDB as a Cake Tool
#tool nuget:?package=Tingle.Extensions.MongoDB&version=4.12.0                

Tingle.Extensions.MongoDB

[!IMPORTANT] The use of MongoDbContext will likely be replaced by EntityFrameworkCore support for MongoDB here.

MongoDB is a cross-platform NoSQL database program which uses JSON like documents with schema.

To connect to the database, we use the MongoClient class to access a MongoDB instance and through it, we can select the database we want to use. With the MongoClient instance we can drop a database, get a database, or retrieve names of the database from the server. There is no option for creating the database because once you pick/select a database and insert data into it, it automatically creates the database if it did not exist before.

MongoClient has a constructor which can accept a connection string or URL as parameters. If the database name is specified in the connection string, a singleton is added to the services. If the database name is specified in the MongoClient constructor parameters, it overrides any database name specified in the connection string.

This library provides extensions for working with MongoDB. It includes extension methods for IServiceCollection and for performing health checks on MongoDB.

Adding to services collection

If a MongoDB client is created that takes a connection string as a parameter, the GetConnectionString() function is called which returns the connection string to the MongoDB.

Also, a MongoDB database contains a collection to store data, To get a reference of the collection, it is required to call the GetCollection() function of the MongoDB database. The GetCollection() function takes a collection name as a parameter and returns a reference to the collection.

Health checks that monitor the performance and functioning of MongoDB are also added to the services as well.

In Program.cs add MongoDB services as shown in the code snippet below:

builder.Services.AddMongoDbContext<MyDbContext>(options => options.UseConnectionString(Configuration.GetConnectionString("Mongo")));

Configuration

If MongoDB client is configured with a connection string, add the ConnectionStrings configuration to the configuration file such as appsettings.json as in the example below:

{
    "ConnectionStrings:Mongo":"#{ConnectionStringsMongo}#"
}

Serializers

Source Types BSON Destination Types
System.Text.Json.Nodes.JsonObject BsonDocument
System.Text.Json.Nodes.JsonArray BsonArray
System.Text.Json.JsonElement BsonDocument, BsonArray, or value
System.Net.IPNetwork (.NET 8 or later) String
Tingle.Extensions.Primitives.Duration String
Tingle.Extensions.Primitives.Etag Int64, Binary or String
Tingle.Extensions.Primitives.SequenceNumber Int64 or String

Diagnostics

Events for Mongo are produced on an ActivitySource named MongoDB.Driver.Core.Extensions.DiagnosticSources, automatically when using MongoDbContext.

HealthChecks

services.AddHealthChecks()
        .AddMongoDbContextCheck<MyContext>();

Database setup

For development and preview environments, databases may need to be created automatically on startup. This can be done using the MONGO_CREATE_DATABASE environment variable.

{
  "profiles": {
    "SerilogSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development",
        "MONGO_CREATE_DATABASE": "true"
      }
    }
  }
}

In the application setup:

builder.Services.AddMongoDatabaseSetup<MyContext>(); // remember to override EnsureCreatedAsync(...) as per sample

Extensions

A number of extensions for building indexes or performing operations on collections exist.

See extensions for building ascending/descending indexes here

See extensions for bulk operations here

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 84 11/19/2024
4.14.1 346 10/14/2024
4.14.0 367 9/16/2024
4.13.0 619 8/13/2024
4.12.0 184 8/7/2024
4.11.2 322 7/15/2024
4.11.1 372 6/26/2024
4.11.0 311 6/6/2024
4.10.1 110 6/5/2024
4.10.0 192 5/27/2024
4.9.0 316 5/16/2024
4.8.0 351 5/5/2024
4.7.0 537 3/25/2024
4.6.0 352 3/8/2024