这个是在查找到的一些资料的基础上自己写的,不足之处请交流指正:
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", );
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();
setting.ConnectionMode = ConnectionMode.Direct;
setting.GuidRepresentation = GuidRepresentation.CSharpLegacy;
setting.IPv6 = false;
setting.MaxConnectionIdleTime = TimeSpan.FromMinutes();
setting.MaxConnectionLifeTime = TimeSpan.FromMinutes();
setting.MaxConnectionPoolSize = ;
setting.SocketTimeout = TimeSpan.FromSeconds();
setting.WaitQueueSize = ;
setting.WaitQueueTimeout = TimeSpan.FromMilliseconds();
} /// <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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
catch (Exception e)
{
throw e;
}
}
}
}