Rabbit.Common.MongoDB
1.1.0
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 Rabbit.Common.MongoDB --version 1.1.0
NuGet\Install-Package Rabbit.Common.MongoDB -Version 1.1.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="Rabbit.Common.MongoDB" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.MongoDB" Version="1.1.0" />
<PackageReference Include="Rabbit.Common.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 Rabbit.Common.MongoDB --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Rabbit.Common.MongoDB, 1.1.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.
#:package Rabbit.Common.MongoDB@1.1.0
#: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=Rabbit.Common.MongoDB&version=1.1.0
#tool nuget:?package=Rabbit.Common.MongoDB&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Rabbit.Common.MongoDB
MongoDB 数据访问组件,提供通用仓储实现。
安装
dotnet add package Rabbit.Common.MongoDB
依赖
- Rabbit.Common.Lite
- MongoDB.Driver 3.5.2
功能
- 通用仓储:
IMongoRepo<T>接口 - CRUD 操作: 完整的增删改查
- 分页查询: 内置分页支持
- LINQ 支持: 强类型查询
快速开始
1. 配置 MongoDB
using Rabbit.Common.MongoDB;
services.AddMongoConfigure(
"mongodb://localhost:27017",
"MyDatabase"
);
2. 定义实体
public class User
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime CreateTime { get; set; }
}
3. 使用仓储
public class UserService
{
private readonly IMongoRepo<User> _userRepo;
public UserService(IMongoRepo<User> userRepo)
{
_userRepo = userRepo;
}
// 新增
public async Task<User> CreateUser(User user)
{
user.CreateTime = DateTime.Now;
return await _userRepo.InsertOneAsync(user);
}
// 查询
public async Task<User?> GetUser(string id)
{
return await _userRepo.FindByIdAsync(id);
}
public async Task<List<User>> GetUsersByAge(int minAge)
{
return await _userRepo.FindAsync(u => u.Age >= minAge);
}
// 分页
public async Task<(List<User> Data, long Total)> GetUsers(int page, int size)
{
return await _userRepo.FindPagedAsync(
_ => true, // 过滤条件
page,
size,
Builders<User>.Sort.Descending(u => u.CreateTime)
);
}
// 更新
public async Task UpdateUser(string id, User user)
{
await _userRepo.UpdateByIdAsync(id, user);
}
// 部分更新
public async Task UpdateUserName(string id, string name)
{
await _userRepo.UpdateOneAsync(
u => u.Id == id,
Builders<User>.Update.Set(u => u.Name, name)
);
}
// 删除
public async Task DeleteUser(string id)
{
await _userRepo.DeleteByIdAsync(id);
}
}
API 参考
IMongoRepo<T>
| 方法 | 说明 |
|---|---|
InsertOneAsync |
插入单条 |
InsertManyAsync |
批量插入 |
FindByIdAsync |
根据 ID 查询 |
FindOneAsync |
查询单条 |
FindAllAsync |
查询全部 |
FindAsync |
条件查询 |
FindPagedAsync |
分页查询 |
CountAsync |
计数 |
UpdateByIdAsync |
根据 ID 更新 |
UpdateOneAsync |
单条更新 |
UpdateManyAsync |
批量更新 |
DeleteByIdAsync |
根据 ID 删除 |
DeleteOneAsync |
单条删除 |
DeleteManyAsync |
批量删除 |
注意事项
- 实体 ID 字段默认使用 MongoDB 的 ObjectId
- 集合名默认为实体类名的小写形式
- 支持自定义集合名
许可证
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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
- MongoDB.Driver (>= 3.7.0)
- Rabbit.Common.Lite (>= 1.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Rabbit.Common.MongoDB:
| Package | Downloads |
|---|---|
|
Rabbit.Common.Full
Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包 |
GitHub repositories
This package is not used by any popular GitHub repositories.