SyZero.MongoDB
1.1.4
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SyZero.MongoDB --version 1.1.4
NuGet\Install-Package SyZero.MongoDB -Version 1.1.4
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="SyZero.MongoDB" Version="1.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.MongoDB" Version="1.1.4" />
<PackageReference Include="SyZero.MongoDB" />
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 SyZero.MongoDB --version 1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.MongoDB, 1.1.4"
#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.
#:package SyZero.MongoDB@1.1.4
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SyZero.MongoDB&version=1.1.4
#tool nuget:?package=SyZero.MongoDB&version=1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.MongoDB
SyZero 框架的 MongoDB 集成模块。
📦 安装
dotnet add package SyZero.MongoDB
✨ 特性
- 🚀 仓储实现 - 基于 MongoDB 的仓储模式实现
- 💾 文档存储 - 原生文档数据库支持
- 🔒 查询构建 - 流畅的查询 API
🚀 快速开始
1. 配置 appsettings.json
{
"MongoDB": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "MyDatabase"
}
}
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();
// 注册服务方式1 - 使用配置文件
builder.Services.AddSyZeroMongoDB();
// 注册服务方式2 - 使用委托配置
builder.Services.AddSyZeroMongoDB(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
options.DatabaseName = "MyDatabase";
});
// 注册服务方式3 - 指定配置节
builder.Services.AddSyZeroMongoDB(builder.Configuration, "MongoDB");
var app = builder.Build();
// 使用SyZero
app.UseSyZero();
app.Run();
3. 使用示例
public class UserService
{
private readonly IRepository<User, string> _userRepository;
public UserService(IRepository<User, string> userRepository)
{
_userRepository = userRepository;
}
public async Task<User> CreateUserAsync(User user)
{
return await _userRepository.InsertAsync(user);
}
public async Task<List<User>> GetActiveUsersAsync()
{
return await _userRepository.GetListAsync(u => u.IsActive);
}
}
📖 配置选项
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
ConnectionString |
string |
"" |
MongoDB 连接字符串 |
DatabaseName |
string |
"" |
数据库名称 |
📖 API 说明
IRepository<TEntity, TPrimaryKey> 接口
| 方法 | 说明 |
|---|---|
GetAsync(id) |
根据主键获取文档 |
GetListAsync(filter) |
根据条件获取文档列表 |
InsertAsync(entity) |
插入文档 |
UpdateAsync(entity) |
更新文档 |
DeleteAsync(id) |
删除文档 |
所有方法都有对应的异步版本(带
Async后缀)
🔧 高级用法
聚合查询
var result = await _userRepository.AggregateAsync(pipeline =>
pipeline
.Match(u => u.IsActive)
.Group(u => u.Department, g => new { Count = g.Count() })
);
索引管理
await _userRepository.CreateIndexAsync(
Builders<User>.IndexKeys.Ascending(u => u.Email),
new CreateIndexOptions { Unique = true }
);
⚠️ 注意事项
- 连接字符串 - 确保 MongoDB 服务可访问
- 主键类型 - MongoDB 默认使用 ObjectId 作为主键
- 索引 - 为常用查询字段创建索引以提高性能
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- MongoDB.Driver (>= 2.19.0)
- SyZero (>= 1.1.4)
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 |
|---|---|---|
| 1.1.5-dev.1 | 28 | 1/29/2026 |
| 1.1.4 | 90 | 1/2/2026 |
| 1.1.4-dev.2 | 41 | 1/2/2026 |
| 1.1.4-dev.1 | 45 | 12/30/2025 |
| 1.1.3 | 93 | 12/30/2025 |
| 1.1.3-dev.6 | 45 | 12/30/2025 |
| 1.1.3-dev.3 | 106 | 1/19/2024 |
| 1.1.3-dev.2 | 165 | 11/3/2023 |
| 1.1.3-dev.1 | 187 | 3/21/2023 |
| 1.1.2 | 352 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 185 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 172 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 176 | 3/15/2023 |
| 1.1.1 | 318 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 183 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 171 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 182 | 3/14/2023 |
| 1.1.0 | 357 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 178 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 179 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 176 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 180 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 177 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 179 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 178 | 3/14/2023 |
| 1.0.6 | 420 | 3/5/2022 |