首页 > 代码库 > MongoDBHelper

MongoDBHelper

using MongoDB.Driver;using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Linq.Expressions;using System.Text;using System.Threading.Tasks;namespace KyeDePart.Common.BLL{    public class MongoDBHelper<T> where T : class    {        protected MongoClient mongoclient;        protected IMongoDatabase database;        protected IMongoCollection<T> collection;        //public static MongoDBHelper<T> MongoDB=new MongoDBHelper<T>();        /// <summary>        /// 初始化操作        /// </summary>        public MongoDBHelper()        {            try            {                mongoclient = new MongoDB.Driver.MongoClient(ConfigurationSettings.AppSettings["MongoConnect"]);                database = mongoclient.GetDatabase(ConfigurationSettings.AppSettings["MongoDatabase"]);                collection = database.GetCollection<T>(typeof(T).Name);            }            catch (Exception ex)            {                Common.WriteLogFile(ex.ToString());            }        }        /// <summary>        /// MongoDB 语法        /// </summary>        /// <param name="filter"></param>        /// <returns></returns>        public T Query(FilterDefinition<T> filter)        {            return collection.Find(filter).FirstOrDefaultAsync().Result;        }        /// <summary>        /// Linq 语法         /// </summary>        /// <param name="func"></param>        /// <returns></returns>        public T Query(Expression<Func<T, bool>> func)        {            //collection.Find(func).ForEachAsync(x =>Console.WriteLine(""));            return collection.Find(func).FirstOrDefaultAsync().Result;        }        public List<T> QueryList(FilterDefinition<T> filter)        {            //var s = collection.Find(filter).ForEachAsync(x => Console.WriteLine(""));            return collection.Find(filter).ToListAsync().Result;        }        public List<T> QueryList(Expression<Func<T, bool>> func)        {            return collection.Find(func).ToListAsync().Result;        }        /// <summary>        /// 分页查询        /// Skip 性能不高        /// </summary>               /// <returns></returns>        public List<T> QueryList(int PageIndex, int PageSize, Expression<Func<T, bool>> func, out long RecordCount)        {            RecordCount = collection.Find(func).Count();            //方法一:            return collection.AsQueryable<T>().Where(func).OrderByDescending(t => "_id").Skip(PageIndex * PageSize).Take(PageSize).ToList();                   }        public bool IsExist(FilterDefinition<T> filter)        {            if (collection.Find(filter).FirstAsync().Result != null)                return true;            else                return false;        }        public bool IsExist(Expression<Func<T, bool>> func)        {            if (collection.Find(func).FirstOrDefaultAsync().Result != null)                return true;            else                return false;            //long count = collection.CountAsync(func).Result;            //if (count > 0)            //    return true;            //else            //    return false;        }        public void Add(T model)        {            collection.InsertOneAsync(model);        }        public void Delete(Expression<Func<T, bool>> func)        {            collection.DeleteOneAsync(func);        }        public void Delete(FilterDefinition<T> filter)        {            collection.DeleteOneAsync(filter);        }        public void DeleteMany(Expression<Func<T, bool>> func)        {            collection.DeleteMany(func);        }        public void Update(FilterDefinition<T> filter, UpdateDefinition<T> updated)        {            collection.UpdateOneAsync(filter, updated);        }        public void UpdateMany(FilterDefinition<T> filter, UpdateDefinition<T> updated)        {            collection.UpdateManyAsync(filter, updated);        }        public void Update(Expression<Func<T, bool>> func, UpdateDefinition<T> updated)        {            collection.UpdateOneAsync(func, updated);        }        public void UpdateMany(Expression<Func<T, bool>> func, UpdateDefinition<T> updated)        {            collection.UpdateManyAsync(func, updated);        }    }}

  Model

 public class PerSon    {        public ObjectId _id;        public string Name { get; set; }        public int Age { get; set; }    }

  

使用方法

public class PerSonBLL    {        protected static MongoDBHelper<Models.PerSon> mongoDB = new MongoDBHelper<Models.PerSon>();        public static List<Models.PerSon> GetList()        {            return mongoDB.QueryList(t => t.Age > 10);        }    }

  

MongoDBHelper