XYS.Mongo 3.1.0

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

XYS.Mongo

基于 MongoDB.Driver 的通用数据访问库:泛型仓储 + 上下文工厂,异步优先。

特性

  • MongoContext 入口:按连接串缓存 IMongoClient(官方推荐的单例复用),分发集合/仓储/索引管理器;FromConfig 接 xys.config.json。
  • MongoRepository<T> 泛型仓储:CRUD 同步 + 异步(异步带 CancellationToken),消费者可继承扩展。
  • MongoRepositoryManager<T> / MongoRepositoryManager<T, TKey> 索引管理:EnsureIndex/DropIndex/IndexExists 等,同步 + 异步。
  • 驱动异常原样冒泡(不吞不包装);配置错误(连接串为空或非法、配置文件缺失或无法读取)统一抛 MongoConfigException。
  • 多目标:net8.0 / net462 / netcoreapp3.1 / netstandard2.0 / netstandard2.1。

快速开始

using XYS.Mongo;
using XYS.Mongo.Repository;

// 1. 从 xys.config.json 的 connectionStrings 节点构造
var ctx = MongoContext.FromConfig("sms");

// 2. 取仓储
var repo = ctx.Repository<SmsSendME>();

// 3. 异步 CRUD
await repo.AddAsync(new SmsSendME { /* ... */ });
var list = await repo.FindMultipleAsync(repo.FilterBuiler.Eq(x => x.Status, 0));
long n = await repo.CountAsync();

继承用法

派生仓储/索引管理器使用 protected 的 (IMongoDatabase, string collectionName = null) 构造,无需再用已过时的连接串构造:

public class SmsSendReqRepository : MongoRepository<SmsSendME>
{
    public SmsSendReqRepository(MongoContext ctx) : base(ctx.Database) { }
    // 或指定集合名:base(ctx.Database, "SmsSend")
}

// 使用:可放进 DI 单例,或自行缓存静态实例
var repo = new SmsSendReqRepository(MongoContext.FromConfig("sms"));

实体与集合名

  • 实体实现 IEntity<TKey> 或继承 BaseME(字符串 ObjectId 主键)。自行实现 IEntity 且 Id 标 [BsonRepresentation(BsonType.ObjectId)] 的实体,按主键的查/删/改与 BaseME 行为一致。
  • 主键约定:
    • GetById/Delete 遇到空主键(null、空白)或非法 ObjectId 时返回 null / 不做任何操作,不抛异常。
    • Update 遇到空主键(null、空白)视为新增,由驱动生成 ObjectId;非空但非法的 ObjectId 主键抛 ArgumentException。
  • 集合名解析顺序:[CollectionName("...")] 特性 → BaseME 继承链顶层类名 → 类型名。

⚠️ 中间基类陷阱:集合名取的是 BaseME 正下方那一层类名。若 LabReportME、ExamReportME 都继承抽象类 AuditME : BaseME,两者会写进同一个 AuditME 集合,LabReportME 的仓储会把检查报告当成检验报告读出(多出的字段被忽略、缺的字段为 null,不报错)。存在中间基类时,每个具体实体都必须用 [CollectionName("...")] 显式指定集合名。

配置(xys.config.json)

"connectionStrings": {
  "sms": {
    "connectionString": "mongodb://user:pwd@host:27017/smsdb"
  }
}

连接串含库名即可;库名缺失时用 MongoOptions.DatabaseName 覆盖。仓库内 xys.config.json 不写明文凭据。

索引管理

var mgr = ctx.Manager<SmsSendME>();          // 自定义主键:ctx.Manager<T, TKey>()
mgr.EnsureIndex("Status");
bool ok = mgr.IndexExists("Status");
mgr.DropIndex("Status");                     // 升序/降序单字段索引一并删除;也可传自定义索引名

异常

try { var ctx = MongoContext.FromConfig("sms"); }
catch (MongoConfigException ex) { /* 配置问题,InnerException 为原始异常 */ }

3.0.0 的 MongoConfigurationException 与 MongoDB.Driver.MongoConfigurationException 重名,同时 using 两个命名空间时 catch 会报 CS0104。3.1.0 起它改为 MongoConfigException 的子类并标 [Obsolete],库内仍抛该类型,旧的 catch 照常生效;新代码请捕获 MongoConfigException。

3.1.0 变更

  • 修复:自行实现 IEntity 且 Id 以 ObjectId 存储的实体,GetById 返回 null、Delete 无效、Update 抛 MongoWriteException(主键过滤改由驱动按 Id 成员序列化器渲染)。
  • 修复:GetById("abc") 抛 IndexOutOfRangeException、GetById(null) 抛 ArgumentNullException;现返回 null。Delete 同理改为无操作。
  • 修复:Update 遇到空字符串主键抛 IndexOutOfRangeException;现视为新增。非空非法 ObjectId 抛 ArgumentException。
  • 修复:派生仓储/索引管理器只能用已过时的连接串构造;(IMongoDatabase, string) 构造改为 protected internal。
  • 修复:配置异常与驱动同名(CS0104),且非法连接串、配置文件缺失抛的是其他异常类型;新增 MongoConfigException 并统一包装。
  • 修复:DropIndexAsync 内部做同步网络请求;改为全程异步。
  • 修复:同字段升降序索引并存时 DropIndex 只删其一;现一并删除。
  • 新增:MongoContext.Manager<T, TKey>(),自定义主键实体也能获取索引管理器。
  • 文档:补充中间基类导致集合共享的警告。

3.0.0 变更(不兼容点)

  • 新增 MongoContext 工厂层,按连接串缓存 MongoClient(修复旧版每仓储实例新建 client 的反模式)。
  • 修复必崩 Bug:Count()/Count(predicate) 自递归爆栈、DeleteAsync(TKey) 假异步、DeleteAllAsync() 为 async void。
  • Delete*Async 返回类型 void → Task。
  • IRepository<T,TKey> 去掉 IQueryable<T> 基接口,改为 AsQueryable() 返回 IMongoQueryable<T>。
  • 删除面向 MMAPv1 的 MongoRepositoryManager 统计/校验(GetStats/Validate/ReIndex/GetTotalDataSize/GetTotalStorageSize/IsCapped)——在 WiredTiger 上本就抛异常。
  • 旧 MongoRepository 构造函数标 [Obsolete](仍可用,内部改走缓存 client)。
Product 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 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 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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on XYS.Mongo:

Package Downloads
XYS.Lab.Mongo

Package Description

XYS.His.Hos.Mongo

Package Description

XYS.Message.Mongo

Package Description

XYS.Emr.Mongo

Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 96 11/18/2026
3.0.0 178 7/23/2026
2.1.0.2 834 1/12/2023
2.1.0.1 565 10/16/2022
2.0.2.2 623 12/20/2021
2.0.1.3 5,057 5/28/2021
2.0.1.2 1,794 4/25/2021
2.0.1.1 569 4/22/2021
2.0.0.1 539 4/22/2021
1.0.0.10 1,117 5/27/2020