MongoDB帮助类

public class MongoHelper
    {
        private static readonly string _connectionString = ConfigHelper.GetAppConfig("mongodb");
        private static readonly string _dbName = ConfigHelper.GetAppConfig("mongodbname");
        private IMongoDatabase mongodb;
        public MongoHelper()
        {
            MongoClient mc = new MongoClient(_connectionString);
            mongodb = mc.GetDatabase(_dbName);
        }

        //单个查询
        public async Task<T> FindAsync<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = await collections.Find(filter).FirstOrDefaultAsync();
            return result;
        }

        public async Task<BsonDocument> FindAsync(string tablename, FilterDefinition<BsonDocument> filter)
        {
            var collections = mongodb.GetCollection<BsonDocument>(tablename);
            var result = await collections.Find(filter).FirstOrDefaultAsync();
            return result;
        }

        public T Find<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var first = collections.Find(filter).FirstOrDefault();
            return first;
        }

        public T Find<T>(string tablename, Expression<Func<T, bool>> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var first = collections.Find(filter).FirstOrDefault();
            return first;
        }

        public T FirstOrDefault<T>(string tablename, string orderbyname, bool isDescending, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            SortDefinition<T> sd = null;
            if (isDescending)
            {
                sd = Builders<T>.Sort.Descending(orderbyname);
            }
            else
            {
                sd = Builders<T>.Sort.Ascending(orderbyname);
            }
            var first = collections.Find(filter).Sort(sd).FirstOrDefault();
            return first;
        }

        //批量查询
        public async Task<List<T>> FindListAsync<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = await collections.Find(filter).ToListAsync();
            return result;
        }

        public List<T> FindList<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var bsonlist = collections.Find(filter).ToList();
            return bsonlist;
        }

        public List<T> FindList<T>(string tablename, FilterDefinition<T> filter, string orderbyname, bool isDescending,int limit)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            SortDefinition<T> sd = null;
            if (isDescending)
            {
                sd = Builders<T>.Sort.Descending(orderbyname);
            }
            else
            {
                sd = Builders<T>.Sort.Ascending(orderbyname);
            }
            var bsonlist = collections.Find(filter).Sort(sd).Limit(limit).ToList();
            return bsonlist;
        }

        public List<T2> Aggregate<T, T2>(string tablename, FilterDefinition<T> filter, BsonDocument bd)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var bsonlist = collections.Aggregate().Match(filter).Group<T2>(bd).ToList();
            return bsonlist;
        }

        public List<T> FindPageList<T>(string tablename, FilterDefinition<T> filter, string orderbyname,bool isDescending, int pageIndex, int pageSize)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            int skip = (pageIndex - 1) * pageSize;
            SortDefinition<T> sd = null;
            if (isDescending)
            {
                sd = Builders<T>.Sort.Descending(orderbyname);
            }
            else
            {
                sd = Builders<T>.Sort.Ascending(orderbyname);
            }
            var list = collections.Find(filter).Sort(sd).Skip(skip).Limit(pageSize).ToList();
            return list;
        }

        public List<T> FindList<T>(string tablename, Expression<Func<T, bool>> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var bsonlist = collections.Find(filter).ToList();
            return bsonlist;
        }

        //插入
        public async Task InsertAsync<T>(string tablename, T t)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            await collections.InsertOneAsync(t);
        }

        public void Insert<T>(string tablename, T t)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            collections.InsertOne(t);
        }

        public async Task InsertManyAsync<T>(string tablename, List<T> t)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            await collections.InsertManyAsync(t);
        }

        public void InsertMany<T>(string tablename, List<T> t)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            collections.InsertMany(t);
        }

        public async Task InsertManyAsync(string tablename, List<BsonDocument> t)
        {
            var collections = mongodb.GetCollection<BsonDocument>(tablename);
            await collections.InsertManyAsync(t);
        }

        public void InsertMany(string tablename, List<BsonDocument> t)
        {
            var collections = mongodb.GetCollection<BsonDocument>(tablename);
            collections.InsertMany(t);
        }

        //批量删除
        public long DeleteMany<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = collections.DeleteMany(filter);
            return result.DeletedCount;
        }

        public async Task<long> DeleteManyAsync<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = await collections.DeleteManyAsync(filter);
            return result.DeletedCount;
        }

        //单个删除
        public bool Delete<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = collections.DeleteOne(filter);
            return result.DeletedCount == 1;
        }

        public bool Delete<T>(string tablename, Expression<Func<T, bool>> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = collections.DeleteOne(filter);
            return result.DeletedCount >= 1;
        }

        public async Task<bool> DeleteAsync<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = await collections.DeleteOneAsync(filter);
            return result.DeletedCount >= 1;
        }

        public async Task<bool> DeleteAsync<T>(string tablename, Expression<Func<T, bool>> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = await collections.DeleteOneAsync(filter);
            return result.DeletedCount >= 1;
        }

        //获取数量
        public long Count<T>(string tablename, FilterDefinition<T> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            return collections.Count(filter);
        }

        public long Count<T>(string tablename, Expression<Func<T, bool>> filter)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            return collections.Count(filter);
        }

        public long Count(string tablename, FilterDefinition<BsonDocument> filter)
        {
            var collections = mongodb.GetCollection<BsonDocument>(tablename);
            return collections.Count(filter);
        }

        //去重查询
        public void Distinct(string tablename, FieldDefinition<BsonDocument, BsonString> field, FilterDefinition<BsonDocument> filter)
        {
            var collections = mongodb.GetCollection<BsonDocument>(tablename);
            collections.Distinct(field, filter);
        }

        //更新数据
        public bool UpdateOne<T>(string tablename, FilterDefinition<T> filter, UpdateDefinition<T> update)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = collections.UpdateOne(filter, update);
            return result.MatchedCount>=1;
        }

        //批量更新
        public bool UpdateMany<T>(string tablename, FilterDefinition<T> filter, UpdateDefinition<T> update)
        {
            var collections = mongodb.GetCollection<T>(tablename);
            var result = collections.UpdateMany(filter, update);
            return result.MatchedCount >= 1;
        }
    }

  

(0)

相关推荐

  • 记一次Mongodb数据库更新操作之更新为数组或者对象

    一直以来都是更新为一些简单的基础类型,直到有一天写了一个覆盖某一个字段(这个字段为数组)的更新操作.出问题了,数据库中出现了_t,_v--有点懵了.当然如果我们更新的时候设置类型是不会出现这个问题的, ...

  • C# ORM学习笔记:Dapper基本用法

    一.基础知识 1.1.Dapper简介 Dapper是.NET下的一个micro ORM,它和Entity Framework或NHibnate不同,属于轻量级并且是半自动的(实体类都要自己写).假如 ...

  • 打造独特的ORM开发框架

    ORM一直是长久不衰的话题,各种重复造轮子的过程一直在进行,轮子都一样是圆的,你的又有什么特点呢? CRL这个轮子造了好多年,功能也越来越标准完备,在开发过程中,解决了很多问题,先上一张脑图描述CRL ...

  • Linq操作ArrayList

    ArrayList实现了System.Collections空间下的IEnumerable接口,这个接口是非泛型的.如果要使用LINQ,必须声明枚举变量的类型,依赖Cast查询运算符转换枚举类型. u ...

  • 表单生成器(Form Builder)之mongodb表单数据查询——返回分页数据和总条数

    上一篇笔记将开始定义的存储结构处理了一下,将FormItems数组中的表单项都拿到mongodb document的最外层,和以前的关系型数据类似,之不过好多列都是动态的,不固定,不过这并没有什么影响 ...

  • 国医大师肖少卿:常用配穴主治方49类,涵盖100多种病症经验配穴

    大家好,我是九选艾的小九老师. 相信很多灸友日常艾灸中最困惑的就是关于配穴的问题,什么病灸什么穴位?当然,最好还是需要专业中医师详细辩证以后,方能给予有效合理的配穴方案. 前几日,朋友赠我一本国医大师 ...

  • 5类降压药物有何区别?男性和女性如何选择降压药?一文搞定!

    作者:高丽丽 来源:药评中心 高血压是一种常见的慢性病.女性高血压受年龄.月经周期.生育及疾病过程和特殊药物等多方面影响,比男性高血压更复杂.那么,男性高血压和女性高血压患者,在选择降压药方面有什么区 ...

  • 精集体站街:半路经典实战7类图形

    深山老牛 楼主 2019-05-10 08:31 最近很多朋友对于个股该低吸还是追高混淆,前面老牛把自己低吸的手法全部公布给了大家,以后就重点讲讲半路追涨做首板的一些方法.便于区分和选股. 以下,是我 ...

  • 企业税负降至2.5%!国家刚宣布!这类企业恭喜了

    刚刚,企业所得税又降了!税负低至2.5%!国家刚宣布:这类企业加计扣除100万=200万! 01 定了!小微企业税负降至2.5%! 今年1月起执行! 国务院总理李克强召开会议再次明确:小微企业和个体户 ...

  • 为何医生让他把氨氯地平换成缬沙坦?药师详解两类降压药的好与坏

    硝苯地平.氨氯地平.缬沙坦.氯沙坦等等,这些降压药都是高血压患者常用的降压药.从名字中也可以看出这些降压药属于两类不同的降压药,一种是地平类,即为钙离子拮抗剂(CCB),另外一种是沙坦类,即为血管紧张 ...

  • 15种权威医师都在开的联合用药,效果出类...

    15种权威医师都在开的联合用药,效果出类拔萃,收藏起来,受益无穷! 1.上清片+牛黄解毒片---出类拔萃---牙痛 2.复方胆通胶囊+加替沙星---出类拔萃---胆结石 3.褪黑素+更年安片---出类 ...

  • 初中数学19类最值问题全覆盖,收藏学习!

    春熙初中数学 25篇原创内容 公众号 初中数学解题思路 本号致力于初中数学学习的钻研和探索.全面覆盖初中数学典型题集.解题模型.动点最值.思路方法.超级易错.几何辅助线.压轴破解等方面,欢迎关注! 1 ...

  • 几何探究类压轴题:精编20例及详解

    成才路上 初中精品学习资料 104篇原创内容 公众号 / END /

  • 高考物理11类重点题型全解析! 附经典例题&详解

    高考理科综合卷中,物理部分选择题有单项和双项选择题两种题型.从最近几年的试题看: 4道单项选择难度低,考查的考点相对稳定且相对单一,涉及的知识点主要有共点力平衡.热力学第一定律.气体状态方程.分子动理 ...