C#操作MongoDB的帮助类-简单封装

  之前做项目,需要记录一些请求URL,响应时间,异常日志,变更日志等信息,项目本身用的是Sql Server,考虑到这些信息增长速度快,占用空间大,且平时除了统计下网站信息,查看下日志,并不会进行关联查询和操作,所以决定这些信息不配录入我的主库,当时百度上看了看,发现MongoDB不错,所以用了2个小时研究了下,就写入项目中了。。。

  好了,不多说,直接把DLL和代码记下来,以便以后直接使用。

  C#版的MongoDB驱动是用官方提供的mongo-csharp-driver

  具体下载地址:http://github.com/mongodb/mongo-csharp-driver/downloads

  引用:MongoDB.Driver.dll,MongoDB.Bson.dll

  

  具体代码部分:

    /// <summary>
    /// MongoDB帮助类
    /// </summary>
    public class MongoDBHelper
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private const string strconn = "mongodb://127.0.0.1:27017";

        /// <summary>
        /// 数据库名称
        /// </summary>
        private const string dbName = "pxdnet";

        MongoServer server = new MongoClient(strconn).GetServer();

        static MongoDatabase db;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MongoDBHelper()
        {
            if(db == null)
            {
                db = server.GetDatabase(dbName);
            }
        }

        /// <summary>
        /// 获取数据库
        /// </summary>
        /// <returns></returns>
        public static MongoDatabase GetDB()
        {
            return new MongoClient(strconn).GetServer().GetDatabase(dbName);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public static T Add<T>(T entity)
        {
            var col = GetDB().GetCollection(typeof(T).Name);
            col.Insert<T>(entity);
            return entity;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="id"></param>
        public static void Update<T>(T entity, ObjectId id)
        {
            var col = GetDB().GetCollection(typeof(T).Name);

            BsonDocument bsd = BsonExtensionMethods.ToBsonDocument(entity);

            IMongoQuery query = Query.EQ("_id", id);

            col.Update(query, new UpdateDocument(bsd));
        }

        /// <summary>
        /// 删除
        /// remark:条件删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dics"></param>
        public static void Delete<T>(Dictionary<string, object> dics)
        {
            var col = GetDB().GetCollection(typeof(T).Name);

            var query = new QueryDocument(dics);

            var result = col.Remove(query);
        }

        /// <summary>
        /// 删除
        /// remark:根据ObjectId删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        public static void Delete<T>(ObjectId id)
        {
            var col = GetDB().GetCollection(typeof(T).Name);
            IMongoQuery query = Query.EQ("_id", id);
            col.Remove(query);
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> QueryList<T>()
        {
            var col = GetDB().GetCollection(typeof(T).Name);

            var result = col.FindAllAs<T>();

            return result.ToList<T>();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dics"></param>
        /// <returns></returns>
        public static List<T> QueryList<T>(Dictionary<string,object> dics)
        {
            var col = GetDB().GetCollection(typeof(T).Name);
            var query = new QueryDocument(dics);

            var result = col.FindAs<T>(query);

            return result.ToList<T>();
        }
    }
View Code

 

  由于用到的功能不多,主要是插入和查询,偶尔记录下响应时间会用到更新操作,所以简单的封装了增删改查,如果需要更多其他操作的朋友,可以看下这篇帖子,介绍的挺全的。

  https://www.cnblogs.com/cuihongyu3503319/p/9354209.html

posted @ 2020-04-07 00:09  鹏小呆  阅读(...)  评论(...编辑  收藏