C#工具类(一):使用.Net封装MongodbHelper

作者:陆金龙    发表时间:2015-05-12 01:54   


C#对Mongodb操作工具类的封装。

需要引用MongoDB.Driver.dll和MongoDB.Bson.dll两个库。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;


namespace Kinglong.AppServer.Tools
{
    public class MongodbHelper
    {

        #region 查询相关

        /// 
        /// 根据条件查询相应结果
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public IEnumerable<T> ExecuteQuery<T>(string conn, string dbname, Dictionary<string, object=""> where) where T : class
        {

            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                QueryDocument qd = new QueryDocument();
                qd.Add(where);
                var result = table.Find(qd);
                server.Disconnect();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 获取相应操作表
        /// 
        /// 
        ///
        ///
        /// 
        static public MongoCollection<T> GetCollection<T>(string conn, string dbname) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                return table;
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }

        /// 
        /// 获取操作表
        /// 
        ///
        ///
        ///
        /// 
        static public MongoCollection GetCollection(string conn, string dbname, string tabName)
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection table = db.GetCollection(tabName);
                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 根据条件查询指定列
        /// 
        /// 
        ///
        ///
        ///
        ///
        /// 
        static public IEnumerable<T> ExecuteQuery<T>(string conn, string dbname, Dictionary<string, object> where, List<string> colunms) where T : class
        {

            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                QueryDocument qd = new QueryDocument();
                qd.Add(where);
                //构建查询列名
                FieldsDocument queryField = new FieldsDocument();
                foreach (var item in colunms)
                {
                    BsonDocument bd = new BsonDocument();
                    bd.Add(item, 1);
                    queryField.Add(bd);
                }
                var result = table.Find(qd).SetFields(queryField);
                server.Disconnect();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 查询表全部信息
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public IEnumerable<T> ExecuteQuery<T>(string conn, string dbname) where T : class
        {

            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                var result = table.FindAll();
                server.Disconnect();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 根据条件查询单个实体对象
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public T ExecuteSingleQuery<T>(string conn, string dbname, Dictionary<string, object> where) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                QueryDocument qd = new QueryDocument();
                qd.Add(where);
                var result = table.FindOne(qd);
                server.Disconnect();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        #endregion

        #region 插入相关
        /// 
        /// 插入单个对象
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public bool ExecuteInsert<T>(string conn, string dbname, T t) where T : class
        {
            MongoServer server = (new MongoClient(conn)).GetServer();
            try
            {
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                table.Insert(t);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        /// 
        /// 插入单个对象文档
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public bool ExecuteInsertBsonDocument<T>(string conn, string dbname, BsonDocument bd) where T : class
        {
            MongoServer server = (new MongoClient(conn)).GetServer();
            try
            {
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                table.Insert(bd);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 指定表名插入bson文档
        /// 
        ///
        ///
        ///
        ///
        /// 
        static public bool ExecuteInsertBsonDocument(string conn, string dbname, string tableName, BsonDocument bd)
        {
            MongoServer server = (new MongoClient(conn)).GetServer();
            try
            {
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                var table = db.GetCollection(tableName);
                table.Insert(bd);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 插入指定集合
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public bool ExecuteInsert<T>(List<T> tList, string conn, string dbname) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                table.InsertBatch(tList);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region 删除相关

        /// 
        /// 删除指定表全部信息
        /// 
        /// 
        ///
        ///
        /// 
        static public bool ExecuteDelete<T>(string conn, string dbname) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                table.RemoveAll();
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 复杂删除
        /// 
        /// 
        ///
        ///
        ///
        /// 
        static public bool ExecuteDelete<T>(string conn, string dbname, Dictionary<string, object> where) where T : class
        {

            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                QueryDocument qd = new QueryDocument();
                qd.Add(where);
                table.Remove(qd);

                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }
        #endregion

        #region 更新相关

        /// 
        /// 更新单个对象
        /// 
        /// 
        ///
        /// 
        static public bool ExecuteUpdateSingle<T>(string conn, string dbname, Dictionary<string, object> where, Dictionary<string, object> set) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                //定义查询条件
                QueryDocument query = new QueryDocument();
                query.Add(where);
                //定义更新文档
                UpdateDocument updateSets = new UpdateDocument();
                updateSets.Add(set);
                var update = new UpdateDocument {
                { "$set", updateSets } };

                table.FindAndModify(query, SortBy.Null, update);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// 
        /// 更新数据表
        /// 
        /// 
        ///
        /// 
        static public bool ExecuteUpdate<T>(string conn, string dbname, Dictionary<string, object> where, Dictionary<string, object> set) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                QueryDocument query = new QueryDocument();
                query.Add(where);
                var update = new UpdateDocument {
                { "$set",(new BsonDocument()).Add(set) } };
                table.Update(query, update, UpdateFlags.Multi);
                server.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region 其他
        static public long ExecuteCount<T>(string conn, string dbname, Dictionary<string, object> where) where T : class
        {
            try
            {
                MongoServer server = (new MongoClient(conn)).GetServer();
                server.Connect();
                MongoDatabase db = server.GetDatabase(dbname);
                MongoCollection<T> table = db.GetCollection<T>(typeof(T).Name.Replace("Info", ""));
                //构建查询条件
                QueryDocument qd = new QueryDocument();
                qd.Add(where);

                server.Disconnect();
                return table.Count(qd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}