C# mongodb数据库操作类 MongoDBHelper

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB;
using MongoDB.Configuration;
using System.Linq.Expressions;
 
namespace Helper
{
    /// 
    /// samus驱动 db通用类
    /// 
    /// 
    public class MongoDBHelper where T : class
    {
        #region property
        private const string connectionString = "mongodb://127.0.0.1:27017";
        private const string databaseName = "Mongodb_DataManager";
        private Mongo mongo;
        private MongoDatabase mongoDatabase;
        private MongoCollection mongoCollection;
        #endregion
 
        #region 构造
        public MongoDBHelper(string name)
        {
            mongo = GetMongo();
            mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
            mongoCollection = mongoDatabase.GetCollection(name) as MongoCollection;
            mongo.Connect();
        }
        public MongoDBHelper()
        {
            mongo = GetMongo();
            mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
            mongoCollection = mongoDatabase.GetCollection() as MongoCollection;
            mongo.Connect();
        }
        ~MongoDBHelper()
        {
            mongo.Disconnect();
        }
        #endregion
 
        #region 配置Mongo,将类UserInfo映射到集合
 
        /// 
        /// 配置Mongo,将类UserInfo映射到集合
        /// 
        private Mongo GetMongo()
        {
            var config = new MongoConfigurationBuilder();
            config.Mapping(mapping =>
            {
                mapping.DefaultProfile(profile =>
                {
                    profile.SubClassesAre(t => t.IsSubclassOf(typeof(T)));
                });
                mapping.Map();
            });
            config.ConnectionString(connectionString);
            return new Mongo(config.BuildConfiguration());
        }
        #endregion
 
        #region methods
 
        #region add
        /// 
        /// 新增
        /// 
        /// 
        public void Insert(T t)
        {
              mongoCollection.Insert(t); 
        }
        /// 
        /// 新增
        /// 
        /// 
        public void Insert(T t,bool b)
        {
            mongoCollection.Insert(t,b);
        }
        #endregion
 
        #region update
        /// 
        /// 更新
        /// 
        /// 
        public void Insert(T t, Expression> func)
        {
            mongoCollection.Update(t, func, true);
        }
        #endregion
 
        #region delete
 
        /// 
        /// 删除
        /// 
        /// 
        public void Delete(Expression> func)
        {
            mongoCollection.Remove(func);
        }
 
        #endregion
 
        #region search
 
 
        /// 
        /// 查询单条数据
        /// 
        /// 
        public T SelectOne(Document spec)
        {
            return mongoCollection.FindOne(spec);
          
        }
        public T SelectOne(Expression> func)
        {
            return mongoCollection.Linq().FirstOrDefault(func);
        }
 
        /// 
        /// 查询集合数据
        /// 
        /// 
        public List SelectMore(Document spec)
        {
            return mongoCollection.Find(spec).Documents.ToList();
        }
        public List SelectMore(Expression> func)
        {
            return mongoCollection.Linq().Where(func).ToList();
        }
 
        /// 
        /// Cursor查询所有
        /// 
        /// 
        public Cursor SelectALl2Cursor()
        {
            return (Cursor)mongoCollection.FindAll();
        }
        /// 
        /// linq查询所有
        /// 
        /// 
        public List SelectALl()
        {
            return mongoCollection.Linq().ToList();
        }
 
        /// 
        /// 求最大最小值
        /// 
        /// 
        /// 
        /// 
        public T selectMaxMin(string fieldname, IndexOrder order)
        {
            return mongoCollection.FindAll().Sort(fieldname, order).Limit(1).Documents.FirstOrDefault();
        }
 
        /// 
        /// MapReduce
        /// 
        /// 
        /// 
        /// 
        public void CreateMapReduce(string MapFunc, string ReduceFunc, string outcollection)
        {
            mongoCollection.MapReduce()
                 .Map(MapFunc)
                   .Reduce(ReduceFunc)
                     .Out(outcollection);
        }
 
        #endregion
 
        #endregion
    }
}

 

转载于:https://my.oschina.net/u/200350/blog/843266

你可能感兴趣的:(C# mongodb数据库操作类 MongoDBHelper)