首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。