代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB;
using MongoDB.GridFS;
namespace CompanyName.DepartmentName.Common.Util
{
public class MongoHelper
{
public static readonly string connectionString = "Servers=127.0.0.1:2222;ConnectTimeout=30000;ConnectionLifetime=300000;MinimumPoolSize=8;MaximumPoolSize=256;Pooled=true";
public static readonly string database = "DiDiDataBase";
#region 新增
/// <summary>
/// 插入新数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
/// <param name="entiry"></param>
public static void InsertOne<T>(string collectionName, T entity) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
categories.Insert(entity, true);
mongo.Disconnect();
}
}
/// <summary>
/// 插入多个数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
/// <param name="entiry"></param>
public static void InsertAll<T>(string collectionName, IEnumerable<T> entity) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
categories.Insert(entity, true);
mongo.Disconnect();
}
}
#endregion
#region 更新
/// <summary>
/// 更新操作
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="collectionName">表名</param>
/// <param name="query">条件</param>
/// <param name="entry">新实体</param>
public static void Update<T>(string collectionName, Document entity, Document query) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
categories.Update(entity, query, true);
mongo.Disconnect();
}
}
/// <summary>
/// 更新操作
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="collectionName">表名</param>
/// <param name="query">条件</param>
/// <param name="entry">新实体</param>
public static void UpdateAll<T>(string collectionName, Document entity, Document query) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
categories.Update(entity, query, UpdateFlags.MultiUpdate, true);
mongo.Disconnect();
}
}
#endregion
#region 查询
/// <summary>
/// 获取一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
/// <param name="query"></param>
/// <returns></returns>
public static T GetOne<T>(string collectionName, Document query) where T : class
{
T result = default(T);
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
result = categories.FindOne(query);
mongo.Disconnect();
}
return result;
}
/// <summary>
/// 获取一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
/// <param name="query"></param>
/// <returns></returns>
public static T GetOne<T>(string collectionName, Document query, Document fields) where T : class
{
T result = default(T);
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase friends = mongo.GetDatabase(database);
IMongoCollection<T> categories = friends.GetCollection<T>(collectionName);
result = categories.Find(query, fields).Skip(0).Limit(1).Documents.First();
mongo.Disconnect();
}
return result;
}
/// <summary>
/// 获取一个集合下所有数据
/// </summary>
/// <param name="collectionName"></param>
/// <returns></returns>
public static List<T> GetAll<T>(string collectionName) where T : class
{
List<T> result = new List<T>();