using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace DSFinanceSystem
{
public class DSMongoDBConn
{
/// <summary>
/// 数据库连接(无密码)
/// </summary>
public static string Conn { get; set; } = "mongodb://127.0.0.1:27017";
/// <summary>
/// 数据库名称
/// </summary>
public static string DataBaseName { get; set; } = "DSFinance";
/// <summary>
/// 集合名称
/// </summary>
public static string CollectionName { get; set; } = "Finance";
}
public class DSMongoDB
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="conn"></param>
public static void InitMonogoDB()
{
Client = new MongoClient(DSMongoDBConn.Conn);
DataBase = Client.GetDatabase(DSMongoDBConn.DataBaseName);
}
/// <summary>
/// 定义接口
/// </summary>
public static IMongoDatabase DataBase { get; set; }
/// <summary>
/// 定义客户端
/// </summary>
public static IMongoClient Client { get; set; }
/// <summary>
/// 获取集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <returns></returns>
public static IMongoCollection<T> GetCollection<T>()
{
IMongoCollection<T> collection = DataBase.GetCollection<T>(DSMongoDBConn.CollectionName);
Filter = new BsonDocument();
return collection;
}
public static BsonDocument Filter { get; set; } = new BsonDocument();
public static async Task InsertOneAsync<T>(T t)
{
try
{
var collection = GetCollection<T>();
await collection.InsertOneAsync(t);
}
catch (Exception e)
{
throw;
}
}
public static void InsertOne<T>(T t)
{
try
{
var collection = GetCollection<T>();
collection.InsertOne(t);
}
catch (Exception e)
{
throw;
}
}
/// <summary>
/// 删除第一行数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public static void DeleteOne<T>(string key, object value)
{
try
{
var collection = GetCollection<T>();
collection.DeleteOne(Builders<T>.Filter.Eq(key, value));
}
catch (Exception e)
{
throw;
}
}
public static async Task DeleteOneAsync<T>(string key, object value)
{
try
{
var collection = GetCollection<T>();
await collection.DeleteOneAsync(Builders<T>.Filter.Eq(key, value));
}
catch (Exception e)
{
throw;
}
}
public static void DeleteMany<T>(string key, object value)
{
try
{
var collection = GetCollection<T>();
collection.DeleteMany(Builders<T>.Filter.Eq(key, value));
}
catch (Exception e)
{
throw;
}
}
public static async Task DeleteManyAsync<T>(string key, object value)
{
try
{
var collection = GetCollection<T>();
await collection.DeleteManyAsync(Builders<T>.Filter.Eq(key, value));
}
catch (Exception e)
{
throw;
}
}
public static void UpdateOne<T>(string key, object oldValue, object newValue)
{
try
{
var collection = GetCollection<T>();
var filter = Builders<T>.Filter.Eq(key, oldValue);
var update = Builders<T>.Update.Set(key, newValue);
collection.UpdateOne(filter, update); //UpdateOne修改一条,UpdateMany修改全部
}
catch (Exception e)
{
throw;
}
}
public static void UpdateMany<T>(string key, object oldValue, object newValue)
{
try
{
var collection = GetCollection<T>();
var filter = Builders<T>.Filter.Eq(key, oldValue);
var update = Builders<T>.Update.Set(key, newValue);
collection.UpdateMany(filter, update); //UpdateOne修改一条,UpdateMany修改全部
}
catch (Exception e)
{
throw;
}
}
public static async Task UpdateOneAsync<T>(string key, object oldValue, object newValue)
{
try
{
var collection = GetCollection<T>();
var filter = Builders<T>.Filter.Eq(key, oldValue);
var update = Builders<T>.Update.Set(key, newValue);
await collection.UpdateOneAsync(filter, update); //UpdateOne修改一条,UpdateMany修改全部
}
catch (Exception e)
{
throw;
}
}
public static async Task UpdateManyAsync<T>(string key, object oldValue, object newValue)
{
try
{
var collection = GetCollection<T>();
var filter = Builders<T>.Filter.Eq(key, oldValue);
var update = Builders<T>.Update.Set(key, newValue);
await collection.UpdateManyAsync(filter, update); //UpdateOne修改一条,UpdateMany修改全部
}
catch (Exception e)
{
throw;
}
}
public static List<T> Find<T>(string key, object value)
{
try
{
var collection = GetCollection<T>();
var res = collection.Find(Builders<T>.Filter.Eq(key, value)).ToList();
return res;
}
catch (Exception e)
{
throw;
}
return default;
}
public static List<T> Find<T>()
{
try
{
var collection = GetCollection<T>();
var res = collection.Find(Filter).ToList();
return res;
}
catch (Exception e)
{
throw;
}
return default;
}
/// <summary>
/// 获取前count条数据
/// </summary>
/// <param name="key"></param>
/// <param name="count"></param>
/// <param name="asc">true=正序 , false=反序</param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> Find<T>(string key,int count,bool asc)
{
try
{
var list = Sort<T>(key,count,asc);
return list;
}
catch (Exception e)
{
throw;
}
return default;
}
/// <summary>
/// 排序
/// </summary>
/// <param name="key"></param>
/// <param name="count"></param>
/// <param name="asc"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static List<T> Sort<T>(string key,int count, bool asc = true)
{
var coll = GetCollection<T>();
FilterDefinitionBuilder<T> builderFilter = Builders<T>.Filter;
//排序生成器
SortDefinitionBuilder<T> builderSort = Builders<T>.Sort;
//排序约束 Ascending 正序 Descending 倒序
SortDefinition<T> sort = null;
if (asc)
{
sort = builderSort.Ascending(key);
}
else
{
sort = builderSort.Descending(key);
}
try
{
var result = coll.Find<T>(Filter).Sort(sort).Limit(count).ToList();
return result;
}
catch (Exception)
{
throw;
}
return default;
}
}
}
MongoDB 常用操作