Rabbit.Common.MongoDB 1.3.1

dotnet add package Rabbit.Common.MongoDB --version 1.3.1
                    
NuGet\Install-Package Rabbit.Common.MongoDB -Version 1.3.1
                    
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.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.MongoDB" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="Rabbit.Common.MongoDB" />
                    
Project file
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.3.1
                    
#r "nuget: Rabbit.Common.MongoDB, 1.3.1"
                    
#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.3.1
                    
#: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.3.1
                    
Install as a Cake Addin
#tool nuget:?package=Rabbit.Common.MongoDB&version=1.3.1
                    
Install as a Cake Tool

Rabbit.Common.MongoDB

MongoDB data access component with generic repository pattern. Inject IMongoRepo<T> for CRUD operations or IMongoDatabase for raw access.

MongoDB 数据访问组件,提供通用仓储实现。

安装

dotnet add package Rabbit.Common.MongoDB

依赖

  • Rabbit.Common.Lite
  • MongoDB.Driver 3.5.2

功能

  • 通用仓储: IMongoRepo<T> 接口
  • CRUD 操作: 完整的增删改查
  • 分页查询: 内置分页支持
  • LINQ 支持: 强类型查询

使用方式

Console 项目

using Microsoft.Extensions.DependencyInjection;
using Rabbit.Common.MongoDB;
using MongoDB.Driver;

// 1. 创建 DI 容器
var services = new ServiceCollection();

// 2. 注册 MongoDB
services.AddMongoConfigure(
    "mongodb://localhost:27017",
    "MyDatabase"
);

// 3. 构建服务提供者
var provider = services.BuildServiceProvider();

// 4. 使用方式一:直接操作 IMongoDatabase
var database = provider.GetRequiredService<IMongoDatabase>();
var collection = database.GetCollection<User>("Users");
var users = await collection.Find(_ => true).ToListAsync();

// 使用方式二:使用封装仓储
var repo = provider.GetRequiredService<IMongoRepo<User>>();
var user = await repo.GetByIdAsync(id);
await repo.InsertAsync(newUser);

Web API 项目

using Rabbit.Common.MongoDB;

services.AddMongoConfigure(
    "mongodb://localhost:27017",
    "MyDatabase"
);

定义实体

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; }
}

使用仓储

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 批量删除

注意事项

  1. 实体 ID 字段默认使用 MongoDB 的 ObjectId
  2. 集合名默认为实体类名的小写形式
  3. 支持自定义集合名

许可证

MIT

Product 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.

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.

Version Downloads Last Updated
1.3.1 107 5/28/2026
1.3.0 110 5/19/2026
1.2.1 117 4/7/2026
1.2.0 127 3/27/2026
1.1.0 124 3/12/2026
1.0.0 121 3/12/2026