# Apteryx.MongoDB.Driver.Extend **Repository Path**: code-Institutes/Apteryx.MongoDB.Driver.Extend ## Basic Information - **Project Name**: Apteryx.MongoDB.Driver.Extend - **Description**: 针对MongoDB.Driver的扩展 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 0 - **Created**: 2019-05-06 - **Last Updated**: 2025-05-19 ## Categories & Tags **Categories**: database-dev **Tags**: None ## README # 针对MongoDB.Driver的扩展 ### 安装 Install-Package Apteryx.MongoDB.Driver.Extend ### 使用方法一: public class Account:BaseMongoEntity { public string Name { get; set; } public string Mobile { get; set; } } public class MyDbService:MongoDbService { public MyDbService(IOptionsMonitor options) : base(options){} public IMongoCollection Account => _database.GetCollection("Account"); } public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMongoDB(options => { options.ConnectionString = Configuration.GetConnectionString("MongoDbConnection"); }); //..................... } } [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private MyDbService db = null; public ValuesController(IMongoDbService db) { this.db = (MyDbService)db; } // GET api/values [HttpGet] public ActionResult> Get() { db.Account.Add(new Account(){Name = "张三",Mobile = "13812345678"}); return new string[] { "value1", "value2" }; } } ### 使用方法二: public class Account:BaseMongoEntity { public string Name { get; set; } public string Mobile { get; set; } } public class MyDbService:MongoDbService { public MyDbService(string conn):base(conn){} //public MyDbService(IOptionsMonitor options) : base(options){} public IMongoCollection Account => _database.GetCollection("Account"); } [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult> Get() { var db = new MyDbService("mongodb://user:pwd@xxx.xxx.xxx.xxx:27017/testdb?authSource=admin"); db.Account.Add(new Account(){Name = "张三",Mobile = "13812345678"}); return new string[] { "value1", "value2" }; } }