ScientificBit.MongoDb
2.0.0
See the version list below for details.
dotnet add package ScientificBit.MongoDb --version 2.0.0
NuGet\Install-Package ScientificBit.MongoDb -Version 2.0.0
<PackageReference Include="ScientificBit.MongoDb" Version="2.0.0" />
<PackageVersion Include="ScientificBit.MongoDb" Version="2.0.0" />
<PackageReference Include="ScientificBit.MongoDb" />
paket add ScientificBit.MongoDb --version 2.0.0
#r "nuget: ScientificBit.MongoDb, 2.0.0"
#:package ScientificBit.MongoDb@2.0.0
#addin nuget:?package=ScientificBit.MongoDb&version=2.0.0
#tool nuget:?package=ScientificBit.MongoDb&version=2.0.0
Introduction
The package implements a repository layer over MongoDB collections.
Developers can easily define CRUD operations on a Collection by just
deriving their collection-wise repository classes from BaseRepository class.
How to Integrate
The package is built for .NET 8, 9 and supports MongoDB v6+.
Configure Database
The package defines IServiceCollection extension to easily setup MongoDB.
The developer may use the following methods defined in MongoDbServiceExtension class:
services.AddMongoDb(connectionString, databaseName);
OR
services.AddMongoDb(opts =>
{
opts.DatabaseName = "your-db-name";
opts.ConnectionString = "db-connection-string";
opts.PropertyNamingStyle = PropertyNamingStyles.CamelCase;
});
Define an Entity Model
All entity models should be derived from BaseEntity. A default
collection name will be generated for the entity. If the developer
want to set a custom name, he can use CollectionNameAttribute attribute
when define the entity.
<br/><br/>Example:
using ScientificBit.MongoDb.Entities;
[CollectionName("shopUsers")]
public class User : BaseEntity
{
public string Name {get; set;}
public string Email {get; set;}
}
Please note that, for the above entity definition, a default collection name
would have been users.
Define a Collection Repository
To define a repository class on your collection, you just need
to derive a class from BaseRepository.
Example implementation:
Define interface for users repository
using ScientificBit.MongoDb.Abstractions.Repositories;
public interface IUsersRepository : IBaseRepository<User>
{
}
Define class for users repository
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using ScientificBit.MongoDb.Abstractions.Db;
internal class UsersRepository : BaseRepository<User>, IUsersRepository
{
public UsersRepository(IMongoDbContext dbContext) : base(dbContext)
{
}
public UsersRepository(ILogger<UsersRepository> logger, IMongoDbContext dbContext)
: base(logger, dbContext)
{
}
#region Overrides
protected override IEnumerable<CreateIndexModel<User>> BuildIndexModels()
{
// Define default indexes
var indexModels = base.BuildIndexModels().ToList();
// Add Unique index for Email address
indexModels.Add(
new CreateIndexModel<User>(Builders<User>.IndexKeys.Ascending(user => user.Email),
new CreateIndexOptions {Background = true, Unique = true})
);
return indexModels;
}
protected override bool ShouldUpdateField(string field)
{
// Disallow updating user's email address
return !string.Equals(field, nameof(User.Email), StringComparison.InvariantCultureIgnoreCase);
}
#endregion
}
You may also define an update model to update specific user fields, e.g:
using ScientificBit.MongoDb.Updates;
public class UserUpdateModel : IUpdateModel
{
/// <summary>
/// Allow updates on `Name` field only
/// </summary>
public string? Name { get; set; }
public bool IsValid()
{
// Don't allow empty names
return !string.IsNullOrEmpty(Name);
}
}
Please don't forget to take care of dependency injections. e.g
services.AddScoped<IUsersRepository, UsersRepository>();
That's it! Your MongoDB is configured and you are all set to play with your users collection.
Using a repository
Please review code documentation to know more about CRUD operations.
Documentation
Please refer code and inline docs
Contribution
Contribution guidelines will be added shortly.
Support The Developer
Please support me to help cover ongoing development, infrastructure costs, and the potential expansion of the project (including new features, testing, and documentation).
Contact
Please contact asif@scientificbit.com for any information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
- MongoDB.Entities (>= 24.1.1)
- Newtonsoft.Json (>= 13.0.3)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
- MongoDB.Entities (>= 24.1.1)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ScientificBit.MongoDb:
| Package | Downloads |
|---|---|
|
ScientificBit.MongoDbMQ
A Simple .NET Core based Message Queue which uses MongoDb. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 3.1.1 | 108 | 8/13/2026 | |
| 3.1.0 | 158 | 8/13/2026 | |
| 3.0.3 | 348 | 1/18/2026 | |
| 3.0.2 | 240 | 8/14/2025 | |
| 3.0.1 | 214 | 8/12/2025 | |
| 3.0.0 | 306 | 8/11/2025 | |
| 2.3.0 | 203 | 8/11/2025 | |
| 2.2.2 | 294 | 8/11/2025 | |
| 2.2.1 | 201 | 6/17/2025 | |
| 2.2.0 | 202 | 6/17/2025 | |
| 2.1.0 | 206 | 6/17/2025 | |
| 2.0.1 | 360 | 6/11/2025 | |
| 2.0.0 | 231 | 6/2/2025 | |
| 1.0.1 | 210 | 6/1/2025 | |
| 1.0.0 | 310 | 5/15/2025 | |
| 0.1.1-alpha | 166 | 2/18/2025 | |
| 0.1.0-alpha | 170 | 2/17/2025 |