首页 > 代码库 > MongodbHelper

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

 

MongodbHelper