隐藏

C# / .Net Core 访问MongoDb库

发布:2021/1/4 8:53:36作者:管理员 来源:本站 浏览次数:1027

话不多说直接上代码

连接字符串:

{ "AppSettings": { "mongodb": "mongodb://用户名:密码@IP地址:端口号" }
}

主体代码:

using ABCDEFG.Config;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Mongodb
{
    public class MongoContext
    {
        public MongoContext()
        {
            Client = new MongoClient(ABCDEFG.GetAppSetting("mongodb"));
        }

        public MongoContext(string connectionName)
        {
            Client = new MongoClient(MengTConfig.GetAppSetting(connectionName));
        }

        private MongoClient Client { get; set; }

        private IMongoDatabase DataBase { get => Client.GetDatabase("MengTLog"); }

        public IMongoCollection<T> DbSet<T>() where T : IMongoModel => DataBase.GetCollection<T>("MengTLog.Logger");

    }

    public static class MongoExtend
    {
        public static void Add<T>(this IMongoCollection<T> collenction, T Model) where T : IMongoModel
                  => collenction.InsertOne(Model);

        public static void AddList<T>(this IMongoCollection<T> collenction, List<T> Model) where T : IMongoModel
                 => collenction.InsertMany(Model);

        /// <summary>
        /// 查找第一个
        /// </summary>
        public static T FirstOrDefault<T>(this IMongoCollection<T> collenction,Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            return collenction.Find(expression).FirstOrDefault();
        }

        /// <summary>
        /// 查找符合数据列表
        /// </summary>
        public static List<T> FindToList<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            return collenction.Find(expression).ToList();
        }

        /// <summary>
        /// 删除全部匹配数据
        /// </summary>
        public static void Delete<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            collenction.DeleteManyAsync(expression);
        }


        /// <summary>
        /// 删除一个
        /// </summary>
        public static void DeleteOne<T>(this IMongoCollection<T> collenction, Expression<Func<T, bool>> expression) where T : IMongoModel
        {
            if (expression == null) { throw new ArgumentNullException("参数无效"); }
            collenction.DeleteOneAsync(expression);
        }
    }
}

IMongoModel:

using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mongodb
{
    public partial class IMongoModel
    {
        /// <summary>
        /// 基础ID
        /// </summary>
        public ObjectId _id { get; set; }
    }
}

值得注意的是:需引用MongoDB.BSon与MongoDB.Driver

VS2017若在引用包后,还是无法找到命名空间,重启VS即可。

ABCDEFG.GetAppSetting("mongodb"));读取配置文件