zoukankan      html  css  js  c++  java
  • 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);
            }
    
        }
    

      

  • 相关阅读:
    [重写] 与 [重载]
    [抽象类] 与 [接口]
    (转载)虚函数表实现机制(即多态性实验机制)
    总结:细节问题(C++篇)
    串流类(istrstream)输入行为的探讨
    比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
    JS控制HTML元素的显示和隐藏
    cocos2dx ios iap接入
    关于cocos2dx 2.x lua 中cocos studio 界面,读入时,无法触摸的几点总结
    lua 元表,监控变量赋值及访问,并自动保存
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/6756647.html
Copyright © 2011-2022 走看看