zoukankan      html  css  js  c++  java
  • MongodbHelper

    这个是在查找到的一些资料的基础上自己写的,不足之处请交流指正:

    using MongoDB.Bson;
    using MongoDB.Driver;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    // more infomation please access https://docs.mongodb.com/getting-started/csharp/
    
    namespace MongoDBTest
    {
        public static class MongoDBHelper
        {
            private static MongoServerAddress server = new MongoServerAddress("127.0.0.1", 27017);
            private static MongoClientSettings setting = new MongoClientSettings();
            private static MongoCredential credential = MongoCredential.CreateCredential("databaseName", "username", "password");
            
            private static void ConfigureSettings(MongoClientSettings setting)
            {
                // setting.Credentials = (IEnumerable<MongoCredential>)credential;
                setting.ConnectTimeout = TimeSpan.FromSeconds(20);
                setting.ConnectionMode = ConnectionMode.Direct;
                setting.GuidRepresentation = GuidRepresentation.CSharpLegacy;
                setting.IPv6 = false;
                setting.MaxConnectionIdleTime = TimeSpan.FromMinutes(10);
                setting.MaxConnectionLifeTime = TimeSpan.FromMinutes(30);
                setting.MaxConnectionPoolSize = 50;
                setting.SocketTimeout = TimeSpan.FromSeconds(30);
                setting.WaitQueueSize = 250;
                setting.WaitQueueTimeout = TimeSpan.FromMilliseconds(500);
            }
    
            /// <summary>
            /// use filter to find data from database
            /// </summary>
            /// <typeparam name="TClass">mapping class,according Builder<TClass></typeparam>
            /// <param name="databaseName">database name</param>
            /// <param name="collectionName">collection name</param>
            /// <param name="filter">FilterDefinition,the field of Builders</param>
            /// <param name="sort">SortDefinition,the field of Builders</param>
            /// <returns>List<TClass>,get by ToList() for changing find result</returns>
            public static List<TClass> FindData<TClass>(string databaseName,string collectionName,FilterDefinition<TClass> filter, SortDefinition<TClass> sort=null)
            {
                try
                {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
                    
                    var result = collection.Find(filter).Sort(sort);
                    var test = result.ToList();
                    return test;
    
                }catch(Exception e)
                {
                    throw e;
                }
            }
    
            public static int InserOne<TClass>(string databaseName,string collectionName,TClass tClass)
            {
                try {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
    
                    collection.InsertOne(tClass);
                    return 1;
                }
                catch(Exception e)
                {
                    throw e;
                }
            }
    public static int UpdateMany<TClass>(string databaseName, string collectionName, FilterDefinition<TClass> filter, UpdateDefinition<TClass> update)
            {
                try
                {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
    
                    collection.UpdateMany(filter, update);
    
                    return 1;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
            public static int UpdateOne<TClass>(string databaseName, string collectionName, FilterDefinition<TClass> filter, UpdateDefinition<TClass> update)
            {
                try
                {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
    
                    collection.UpdateOne(filter, update);
    
                    return 1;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
            public static int DeleteOne<TClass>(string databaseName, string collectionName, FilterDefinition<TClass> filter)
            {
                try
                {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
    
                    collection.DeleteOne(filter);
    
                    return 1;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
            public static int DeleteMany<TClass>(string databaseName, string collectionName, FilterDefinition<TClass> filter)
            {
                try
                {
                    ConfigureSettings(setting);
                    var client = new MongoClient(setting);
    
                    var database = client.GetDatabase(databaseName);
                    var collection = database.GetCollection<TClass>(collectionName);
    
                    collection.DeleteMany(filter);
    
                    return 1;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
    }
  • 相关阅读:
    MC9S12 硬件设计
    ESD
    选用与使用稳压二极管的介绍
    MOSFET 栅极电阻作用及其选型
    orcad常用库文件介绍
    开关电源和LDO的区别
    续流二极管的作用及选型
    为什么大电容滤低频小电容滤高频的问题
    Java常用API——时间类
    Idea问题:“marketplace plugins are not loaded”解决方案
  • 原文地址:https://www.cnblogs.com/heisehenbai/p/6852762.html
Copyright © 2011-2022 走看看