.net core 3.1 & Mongodb

部分参考官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-3.1&tabs=visual-studio

  1. nuget
  • 安装 MongoDB.Driver(我操作版本2.10.0)


    mongodb依赖包
  1. 使用(封装什么的这里就不写了,直接写最直接的使用)
    2.1 连接字符串
  • 修改文件 appsettings.Development.json | appsettings.json (开发环境|生产环境)
  • 账号访问 (用户名:密码@连接的值/对映的数据库)
"MongoDatabaseSettings": {
    "ConnectionString": "mongodb://name:password@localhost:27018/myDB",
    "DatabaseName": "myDB"
  },
  • 直接访问
"MongoDatabaseSettings": {
    "ConnectionString": "mongodb://localhost:27018",
    "DatabaseName": "myDB"
  },

2.2 创建连接字Model MongoSettings.cs

    public class MongoDatabaseSettings : IMongoDatabaseSettings
    {
        public string ConnectionString { get; set; }
        public string DatabaseName { get; set; }
    }
    public interface IMongoDatabaseSettings
    {
        string ConnectionString { get; set; }
        string DatabaseName { get; set; }
    }

2.3 创建MongodbService.cs

   public class MongodbService
    {
        // 管理员集合
        public AdminService AdminsCollection;
        // 用户集合
        public UserService UserCollection;
        // 数据库
        public IMongoDatabase Database { get; set; }

        public MongodbService(IMongoDatabaseSettings settings)
        {
            // 创建连接
            var client = new MongoClient(settings.ConnectionString);
            // 获取数据库
            Database = client.GetDatabase(settings.DatabaseName);
            // 获取文档
            AdminsCollection = new AdminService(GetCollection("admins"));
            UserCollection = new UserService(GetCollection("users"));
        }
        // 获取文档
        private IMongoCollection GetCollection(string name) => Database.GetCollection(name);
    }

2.4 注入 Startup.cs

 public void ConfigureServices(IServiceCollection services)
        {
            // 获取配置文件 的 MongoSettings
            services.Configure(
                Configuration.GetSection(nameof(MongoSettings)));
            // 注入连接字符串
            services.AddSingleton(sp =>
                sp.GetRequiredService>().Value);
            // 注入MongodbService
            services.AddSingleton();

            services.AddControllers();
        }

2.5 collertion 中使用

  • 构造函数中注入 MogodbService
  • 就可以使用 MogodbService 中的对象了
public class LoginController : Controller
    {
        private AdminService _adminCollection;
        private MongodbService _mongodbService;
        public LoginController(MongodbService mongodbService)
        {
            _mongodbService = mongodbService;
            _adminCollection = mongodbService.AdminsCollection;
        }
    }

我觉得很实用的两个方法

  • 构造过滤器 (伪代码)
var filter = Builders.Filter.Where(T => T.xx == xx && T.xxx = xxx);
var data = Collection.Find(filter);
  • 字段映射(伪代码)
var filter = Builders.Filter.Where(T => T.xx == xx && T.xxx = xxx);
var projection = Builders.Projection.Expression(u => new T1() { Id = u.Id, Name = u.Name }));
var list = Collection.Find(filter).Project(projection).ToList();

你可能感兴趣的:(.net core 3.1 & Mongodb)