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);
            }
    
        }
    

      

  • 相关阅读:
    死锁:同步中嵌套同步,但锁不同,示例二:
    WinCC VBS利用EXCEL调用Windows API函数
    IE8在上传文件时路径无效或无效的图片文件解决办法【转】
    jquery 资料
    设置倒计时10秒可用的按钮JS函数(转)
    WINCC中数据EXCEL报表的实现方法
    Js 操作radiobuttonlist的方法 (转)
    转自百度 silverlight(转)
    Silverlight五子棋(转)
    两台Oracle服务器,使用udl测试连接
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/6756647.html
Copyright © 2011-2022 走看看