EasilyNET.Mongo.AspNetCore
4.25.613.111
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EasilyNET.Mongo.AspNetCore --version 4.25.613.111
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 4.25.613.111
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="EasilyNET.Mongo.AspNetCore" Version="4.25.613.111" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.Mongo.AspNetCore" Version="4.25.613.111" />
<PackageReference Include="EasilyNET.Mongo.AspNetCore" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasilyNET.Mongo.AspNetCore --version 4.25.613.111
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.Mongo.AspNetCore, 4.25.613.111"
#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.
#addin nuget:?package=EasilyNET.Mongo.AspNetCore&version=4.25.613.111
#tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=4.25.613.111
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.Mongo.AspNetCore
- 一个 MongoDB 驱动的服务包,方便使用 MongoDB 数据库.
- 数据库中字段名驼峰命名,ID,Id 自动转化成 ObjectId.
- 可配置部分类的 Id 字段不存为 ObjectId,而存为 string 类型.支持子对象以及集合成员的 Id 字段转化.
- 自动本地化 MongoDB 时间类型
- 添加.Net6 Date/Time Only 类型支持(序列化到 String 或 long)
- 支持通过特性的方式创建和更新索引
ChangeLogs
- 支持自定义 TimeOnly 和 DateOnly 的格式化格式.
- 支持转换成字符串格式
- 转换成 Ticks 的方式存储
- 若想转化成其他类型也可自行实现,如:转化成 ulong 类型
- 添加动态类型支持[object 和 dynamic], 2.20 版后官方又加上了. JsonArray.
- 添加 JsonNode, JsonObject 类型支持.
添加自定义序列化支持(可选)
JsonNode 类型因为反序列化时不支持 Unicode 字符,如果需要序列化插入至其他地方(例如 Redis),在序列化时需要将 JsonSerializerOptions 的 Encoder 属性设置为 System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping.
builder.Services.AddMongoContext<DbContext>(builder.Configuration)
// 添加自定义序列化
builder.Services.RegisterSerializer(new DateOnlySerializerAsString());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsString());
// 或者将他们存储为long类型的Ticks,也可以自己组合使用.
builder.Services.RegisterSerializer(new DateOnlySerializerAsTicks());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsTicks());
// 添加JsonNode支持
builder.Services.RegisterSerializer(new JsonNodeSerializer());
builder.Services.RegisterSerializer(new JsonObjectSerializer());
使用
- Nuget 安装 EasilyNET.Mongo.AspNetCore
- 在系统环境变量或者 Docker 容器中设置环境变量名称为: CONNECTIONSTRINGS_MONGO = mongodb 链接字符串 或者在 appsettings.json 中添加,
- 现在你也可以参考 example.api 项目查看直接传入相关数据.
- 添加 APM 探针支持,根据 SkyApm.Diagnostics.MongoDB
{
"ConnectionStrings": {
"Mongo": "mongodb链接字符串"
},
// 或者使用
"CONNECTIONSTRINGS_MONGO": "mongodb链接字符串"
}
方法 1. 使用默认依赖注入方式
var builder = WebApplication.CreateBuilder(args);
// 添加Mongodb数据库服务
builder.Services.AddMongoContext<DbContext>(builder.Configuration, c =>
{
// 配置数据库名称,覆盖掉连接字符串中的数据库名称
c.DatabaseName = "test23";
// 配置不需要将Id字段存储为ObjectID的类型.使用$unwind操作符的时候,ObjectId在转换上会有一些问题,所以需要将其调整为字符串.
c.ObjectIdToStringTypes = new()
{
typeof(MongoTest2)
};
// 是否使用默认转换配置.包含如下内容:
// 1.小驼峰字段名称 如: pageSize ,linkPhone
// 2.忽略代码中未定义的字段
// 3.将ObjectID字段 _id 映射到实体中的ID或者Id字段,反之亦然.在存入数据的时候将Id或者ID映射为 _id
// 4.将枚举类型存储为字符串, 如: Gender.男 存储到数据中为 男,而不是 int 类型
c.DefaultConventionRegistry = true;
// 配置自定义Convention
c.ConventionRegistry= new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
// 通过ClientSettings来配置一些使用特殊的东西
c.ClientSettings = cs =>
{
// 对接 SkyAPM 的 MongoDB探针或者别的事件订阅器
cs.ClusterConfigurator = cb => cb.Subscribe(new ActivityEventSubscriber());
};
});
// 添加.NET6+新的TimeOnly和DateOnly数据类型的序列化方案和添加动态类型支持
builder.Services.RegisterSerializer(new DateOnlySerializerAsString());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsString());
// 注册别的序列化方案
builder.Services.RegisterSerializer(new DoubleSerializer(BsonType.Double));
...
var app = builder.Build();
方法 2. 使用 EasilyNET.AutoDependencyInjection
- 项目添加 EasilyNET.AutoDependencyInjection Nuget 包
- 创建 EasilyNETMongoModule.cs 并继承 AppModule 类
public class EasilyNETMongoModule : AppModule
{
/// <summary>
/// 配置和注册服务
/// </summary>
/// <param name="context"></param>
public override void ConfigureServices(ConfigureServicesContext context)
{
var config = context.Services.GetConfiguration();
// 使用 IConfiguration 的方式注册例子,使用链接字符串,仅需将config替换成连接字符即可.
//context.Services.AddMongoContext<DbContext>(config, c =>
//{
// // 配置数据库名称,覆盖掉连接字符串中的数据库名称
// c.DatabaseName = "test23";
// // 配置不需要将Id字段存储为ObjectID的类型.使用$unwind操作符的时候,ObjectId在转换上会有一些问题,所以需要将其调整为字符串.
// c.ObjectIdToStringTypes = new()
// {
// typeof(MongoTest2)
// };
// // 是否使用默认转换配置.包含如下内容:
// // 1.小驼峰字段名称 如: pageSize ,linkPhone
// // 2.忽略代码中未定义的字段
// // 3.将ObjectID字段 _id 映射到实体中的ID或者Id字段,反之亦然.在存入数据的时候将Id或者ID映射为 _id
// // 4.将枚举类型存储为字符串, 如: Gender.男 存储到数据中为 男,而不是 int 类型
// c.DefaultConventionRegistry = true;
// c.ConventionRegistry= new()
// {
// {
// $"{SnowId.GenerateNewId()}",
// new() { new IgnoreIfDefaultConvention(true) }
// }
// };
// // 通过ClientSettings来配置一些使用特殊的东西
// c.ClientSettings = cs =>
// {
// // 对接 SkyAPM 的 MongoDB探针或者别的事件订阅器
// cs.ClusterConfigurator = cb => cb.Subscribe(new ActivityEventSubscriber());
// };
//});
//context.Services.AddMongoContext<DbContext2>(config);
//context.Services.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
// 例子二:使用MongoClientSettings配置
context.Services.AddMongoContext<DbContext>(new MongoClientSettings
{
Servers = new List<MongoServerAddress> { new("127.0.0.1", 27018) },
Credential = MongoCredential.CreateCredential("admin", "guest", "guest"),
// 对接 SkyAPM 的 MongoDB探针
ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber())
}, c =>
{
// 配置数据库名称,覆盖掉连接字符串中的数据库名称
c.DatabaseName = "test23";
// 配置不需要将Id字段存储为ObjectID的类型.使用$unwind操作符的时候,ObjectId在转换上会有一些问题.
c.ObjectIdToStringTypes = new()
{
typeof(MongoTest2)
};
// 是否使用默认转换配置.包含如下内容:
// 1.小驼峰字段名称 如: pageSize ,linkPhone
// 2.忽略代码中未定义的字段
// 3.将ObjectID字段 _id 映射到实体中的ID或者Id字段,反之亦然.在存入数据的时候将Id或者ID映射为 _id
// 4.将枚举类型存储为字符串, 如: Gender.男 存储到数据中为 男,而不是 int 类型
c.DefaultConventionRegistry = true;
c.ConventionRegistry= new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
});
// 注册另一个DbContext
context.Services.AddMongoContext<DbContext2>(config, c =>
{
c.DefaultConventionRegistry = true;
c.ConventionRegistry = new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
});
}
}
- 创建 AppWebModule.cs 并添加 EasilyNETMongoModule
/**
* 要实现自动注入,一定要在这个地方添加
*/
[DependsOn(
typeof(DependencyAppModule),
typeof(EasilyNETMongoModule)
)]
public class AppWebModule : AppModule
{
/// <summary>
/// 注册和配置服务
/// </summary>
/// <param name="context"></param>
public override void ConfigureServices(ConfigureServicesContext context)
{
base.ConfigureServices(context);
_ = context.Services.AddHttpContextAccessor();
}
/// <summary>
/// 注册中间件
/// </summary>
/// <param name="context"></param>
public override void ApplicationInitialization(ApplicationContext context)
{
base.ApplicationInitialization(context);
var app = context.GetApplicationBuilder();
_ = app.UseAuthorization();
}
}
- 最后在 Program.cs 中添加如下内容
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 自动注入服务模块
builder.Services.AddApplication<AppWebModule>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) _ = app.UseDeveloperExceptionPage();
// 添加自动化注入的一些中间件.
app.InitializeApplication();
app.MapControllers();
app.Run();
使用 GridFS
- 注册服务
// 需要提前注册 IMongoDatabase, 或者使用其他重载来注册服务.
builder.Services.AddMongoGridFS();
- 使用依赖注入获取 GridFSBucket 操作 GridFS
public class YourClass(IGridFSBucket bucket)
{
private readonly IGridFSBucket _bucket = bucket;
public void DoSomething()
{
_bucket.XXXXXX();
}
}
使用索引
EasilyNET.Mongo.AspNetCore 支持基于特性自动为实体类创建 MongoDB 索引,且会根据你的字段命名约定(如小驼峰)自动适配索引字段名.
自动索引创建的核心特性:
- 支持在实体属性上使用 [MongoIndex] 特性声明单字段索引.
- 支持在实体类上使用 [MongoCompoundIndex] 特性声明复合索引.
- 支持唯一索引、文本索引、地理空间索引等多种类型.
- 字段名自动适配小驼峰等命名约定,无需手动处理.
使用案例:
public class User
{
[MongoIndex(EIndexType.Ascending, Unique = true)]
public string UserName { get; set; } = string.Empty;
[MongoIndex(EIndexType.Descending)]
public DateTime CreatedAt { get; set; }
}
[MongoCompoundIndex(new[] { "UserName", "CreatedAt" }, new[] { EIndexType.Ascending, EIndexType.Descending }, Unique = true)]
public class Log
{
public string UserName { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
}
最后在中间件中配置对应DbContext的内容:
var app = builder.Build();
// 自动为所有集合创建索引,字段名自动适配小驼峰等命名约定
app.UseCreateMongoIndexes<DbContext>();
// 若存在多个或者集合分布在多个Context中.需要多次应用.
app.UseCreateMongoIndexes<DbContext2>();
注意事项:
- 自动索引创建会比对现有索引定义,若定义不一致会自动删除并重建(通过名称匹配,若是不存在对应名称,将不会删除原有索引[为了避免手动创建的索引失效]).
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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- EasilyNET.Core (>= 4.25.613.111)
- EasilyNET.Mongo.Core (>= 4.25.613.111)
-
net8.0
- EasilyNET.Core (>= 4.25.613.111)
- EasilyNET.Mongo.Core (>= 4.25.613.111)
-
net9.0
- EasilyNET.Core (>= 4.25.613.111)
- EasilyNET.Mongo.Core (>= 4.25.613.111)
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 | |
---|---|---|---|
4.25.617.153 | 123 | 6/17/2025 | |
4.25.616.190 | 123 | 6/16/2025 | |
4.25.616.182 | 127 | 6/16/2025 | |
4.25.613.111 | 263 | 6/13/2025 | |
4.25.609.190 | 799 | 6/9/2025 | |
3.24.929.131 | 279 | 9/29/2024 | |
3.24.929.122 | 268 | 9/29/2024 | |
3.24.926.184 | 273 | 9/26/2024 | |
3.24.926.182 | 270 | 9/26/2024 | |
3.24.926.175 | 269 | 9/26/2024 | |
3.24.924.160 | 275 | 9/24/2024 | |
3.24.924.133 | 276 | 9/24/2024 | |
3.24.924.124 | 264 | 9/24/2024 | |
3.24.924.10 | 271 | 9/23/2024 | |
3.24.924.1 | 271 | 9/23/2024 | |
3.24.923.234 | 264 | 9/23/2024 | |
3.24.923.232 | 269 | 9/23/2024 | |
3.24.923.155 | 269 | 9/23/2024 | |
3.24.919.92 | 269 | 9/19/2024 | |
3.24.914.125 | 437 | 9/14/2024 | |
3.24.914.115 | 299 | 9/14/2024 | |
3.24.914.111 | 270 | 9/14/2024 | |
3.24.911.95 | 268 | 9/11/2024 | |
3.24.908.215 | 262 | 9/8/2024 | |
3.24.904.200 | 420 | 9/4/2024 | |
3.24.828.163 | 272 | 8/28/2024 | |
3.24.820.173 | 275 | 8/20/2024 | |
3.24.814.92 | 284 | 8/14/2024 | |
3.24.812.115 | 276 | 8/12/2024 | |
3.24.802.100 | 254 | 8/2/2024 | |
3.24.801.162 | 265 | 8/1/2024 | |
3.24.801.160 | 262 | 8/1/2024 | |
3.24.801.155 | 263 | 8/1/2024 | |
3.24.730.164 | 254 | 7/30/2024 | |
3.24.730.91 | 256 | 7/30/2024 | |
3.24.724.91 | 257 | 7/24/2024 | |
3.24.718.105 | 269 | 7/18/2024 | |
3.24.716.95 | 267 | 7/16/2024 | |
3.24.712.94 | 264 | 7/12/2024 | |
3.24.710.14 | 264 | 7/9/2024 | |
3.24.709.105 | 266 | 7/9/2024 | |
3.24.704.94 | 272 | 7/4/2024 | |
3.24.701.90 | 273 | 7/1/2024 | |
3.24.628.114 | 276 | 6/28/2024 | |
3.24.627.145 | 267 | 6/27/2024 | |
3.24.620.160 | 275 | 6/20/2024 | |
3.24.613.115 | 270 | 6/13/2024 | |
3.24.612.95 | 266 | 6/12/2024 | |
3.24.528.90 | 267 | 5/28/2024 | |
3.24.522.84 | 277 | 5/22/2024 | |
3.24.512.213 | 272 | 5/12/2024 | |
3.24.508.112 | 291 | 5/8/2024 | |
2.2024.428.71 | 270 | 4/28/2024 | |
2.2024.427.1128 | 269 | 4/27/2024 | |
2.2.72 | 343 | 4/14/2024 | |
2.2.71 | 279 | 4/12/2024 | |
2.2.8 | 266 | 4/26/2024 | |
2.2.6 | 279 | 4/10/2024 | |
2.2.5 | 286 | 3/26/2024 | |
2.2.4 | 286 | 3/25/2024 | |
2.2.3 | 280 | 3/24/2024 | |
2.2.2 | 284 | 3/21/2024 | |
2.2.1 | 285 | 3/20/2024 | |
2.2.0 | 282 | 3/13/2024 | |
2.1.9 | 291 | 2/21/2024 | |
2.1.8 | 280 | 2/18/2024 | |
2.1.7 | 286 | 2/16/2024 | |
2.1.6 | 306 | 2/14/2024 | |
2.1.5 | 283 | 2/14/2024 | |
1.9.1 | 320 | 11/1/2023 | |
1.9.0 | 311 | 10/19/2023 | |
1.9.0-preview2 | 501 | 10/12/2023 | |
1.9.0-preview1 | 270 | 10/12/2023 | |
1.8.9 | 349 | 10/11/2023 | |
1.8.8 | 324 | 10/11/2023 | |
1.8.7-rc2 | 294 | 9/21/2023 | |
1.8.7-rc1 | 282 | 9/12/2023 | |
1.8.6 | 327 | 8/31/2023 | |
1.8.5 | 1,004 | 8/25/2023 | |
1.8.4 | 322 | 8/24/2023 | |
1.8.3 | 327 | 8/23/2023 | |
1.8.2 | 403 | 8/22/2023 | |
1.8.1 | 344 | 8/18/2023 | |
1.8.0 | 322 | 8/15/2023 | |
1.7.9 | 363 | 8/11/2023 | |
1.7.8 | 305 | 8/11/2023 | |
1.7.7 | 351 | 8/10/2023 | |
1.7.6 | 339 | 8/9/2023 | |
1.7.5 | 393 | 8/9/2023 | |
1.7.4 | 427 | 8/3/2023 | |
1.7.3 | 343 | 8/1/2023 | |
1.7.2 | 332 | 7/31/2023 | |
1.7.1 | 334 | 7/27/2023 | |
1.7.0 | 341 | 7/25/2023 | |
1.6.9 | 333 | 7/25/2023 | |
1.6.8 | 328 | 7/24/2023 | |
1.6.7 | 351 | 7/20/2023 | |
1.6.6 | 355 | 7/19/2023 | |
1.6.5 | 314 | 7/19/2023 | |
1.6.4 | 330 | 7/17/2023 | |
1.6.3 | 309 | 7/17/2023 | |
1.6.2 | 370 | 7/12/2023 | |
1.6.1 | 364 | 6/30/2023 | |
1.6.0 | 302 | 6/30/2023 |