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++ 头文件 及 sort 和 vector简单介绍
    最短路径(Floyd 模板题)
    最小生成树(kruskal模版 模板)
    基于邻接矩阵的广度优先搜索遍历(BFS)
    [SCOI2015]国旗计划
    [HNOI2015]开店
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/6756647.html
Copyright © 2011-2022 走看看