zoukankan      html  css  js  c++  java
  • 【MongoDB】MongoHelper

    public class MongoHelper
        {
            private static IMongoDatabase GetDb(string constr, string dbName)
            {
                var db = new MongoClient(constr).GetDatabase(dbName);
                return db;
            }
    
            private static IMongoCollection<T> GetCT<T>(string constr, string dbName, string tableName)
            {
                return GetDb(constr, dbName).GetCollection<T>(tableName);
            }
            public static void Insert<T>(string constr, string dbName,string tableName,T document)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                collection.InsertOne(document);
            }
            public static void Insert<T>(string constr, string dbName, string tableName, IEnumerable<T> documents)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                collection.InsertMany(documents);
            }
    
            public static List<T> Find<T>(string constr, string dbName, string tableName, FilterDefinition<T> filter)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                return collection.Find<T>(filter).ToList<T>();
            }
    
            public static List<T> Find<T>(string constr, string dbName, string tableName, FilterDefinition<T> filter,SortDefinition<T> sort)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                return collection.Find<T>(filter).Sort(sort).ToList<T>();
            }
    
            public static List<T> Find<T>(string constr, string dbName, string tableName,int pageIndex,int pageSize, FilterDefinition<T> filter)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                return collection.Find<T>(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList<T>();
            }
    
            public static bool Update<T>(string constr, string dbName, string tableName,FilterDefinition<T> filter,UpdateDefinition<T> update)
            {
                var collection = GetCT<T>(constr, dbName, tableName);
                var result = collection.UpdateMany(filter, update);
                if (result.ModifiedCount > 0)
                {
                    return true;
                }
                return false;
            }
    
            public static bool Delete(string constr, string dbName, string tableName, BsonDocument filter)
            {
                var collection = GetCT<BsonDocument>(constr, dbName, tableName);
                DeleteResult result = collection.DeleteMany(filter);
                if (result != null && result.DeletedCount > 0)
                {
                    return true;
                }
                return false;
            }
        }
    

      

  • 相关阅读:
    函数指针和回调函数
    MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合
    MATLAB神经网络(1)之R练习
    R时间序列分析实例
    自动控制理论的MATLAB仿真实例(二)
    自动控制理论的MATLAB仿真实例(一)
    Mathtype快捷键&小技巧
    Latex数学符号对应表
    MATLAB神经网络(1) BP神经网络的数据分类——语音特征信号分类
    R语言实战(三) 图形初阶
  • 原文地址:https://www.cnblogs.com/zspbolg/p/5885416.html
Copyright © 2011-2022 走看看