上篇讲了MongoDB的安装和基本操作,今天主要是写了个MongoDB的Helper类,可能以后会用到。MongoDB本身是用C++写的.C#需要通过驱动来调用,主要有以下两种:
官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads。
samus驱动:https://github.com/samus/mongodb-csharp/downloads。
我用的则是samus驱动.
using MongoDB;
using MongoDB.Linq;
using MongoDB.Configuration;
namespace Simple
{
public sealed partial class MongoDBHelper
{
public static string ServerString { get; set; }
public static bool Pooled { get; set; }
public static TimeSpan ConnectionTimeout { get; set; }
public static TimeSpan ConnectionLifetime { get; set; }
public static int MinimumPoolSize { get; set; }
public static int MaximumPoolSize { get; set; }
/// <summary>
/// 实现linq查询的映射配置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static MongoConfigurationBuilder MongoConfigurationBuilerMapping<T>()
where T : class
{
var config = new MongoConfigurationBuilder();
config.Mapping(mapping =>
{
mapping.DefaultProfile(profile =>
{
profile.SubClassesAre(t => t.IsSubclassOf(typeof(T)));
});
mapping.Map<T>();
mapping.Map<T>();
});
MongoConfigurationConnection(config);
return config;
}
/// <summary>
/// 配置MongoDB参数
/// </summary>
/// <param name="config"></param>
public static void MongoConfigurationConnection(MongoConfigurationBuilder config)
{
if (config != null)
{
MongoConfigrationProperty();
Action<MongoConnectionStringBuilder> connectionString = (c) =>
{
c.Pooled = Pooled;
c.AddServer(new MongoServerEndPoint(ServerString));
c.ConnectionTimeout = ConnectionTimeout;
c.ConnectionLifetime = ConnectionLifetime;
c.MinimumPoolSize = MinimumPoolSize;
c.MaximumPoolSize = MaximumPoolSize;
};
config.ConnectionString(connectionString);
}
}
public static void MongoConfigrationProperty()
{
//获取配置文件的各项配置数据
}
/// <summary>
/// 插入单条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="entity"></param>
public static void InsertOne<T>(string dataBaseName, string collectionName, T entity)
where T : class
{
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
collection.Insert(entity, true);
if (mongo.TryConnect()) mongo.Disconnect();
}
}
/// <summary>
/// 插入多条新数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="entitys"></param>
public static void InsertAll<T>(string dataBaseName, string collectionName, IEnumerable<T> entitys)
where T : class
{
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
collection.Insert(entitys, true);
if (mongo.TryConnect()) mongo.Disconnect();
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="selector"></param>
/// <param name="entity"></param>
public static void UpdateOne<T>(string dataBaseName, string collectionName, Expression<Func<T, bool>> selector, T entity)
where T : class
{
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
collection.Update(entity, selector, true);
if (mongo.TryConnect()) mongo.Disconnect();
}
}
/// <summary>
/// 修改多条记录
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="selector"></param>
/// <param name="entity"></param>
public static void UpdateAll<T>(string dataBaseName, string collectionName, Expression<Func<T, bool>> selector, T entity)
where T : class
{
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
collection.Update(entity, selector, UpdateFlags.MultiUpdate, true); //UpdateFlags.Upsert: 如果不存在Update的记录,插objNew
//UpdateFlags.MultiUpdate: 把按条件查出来多条记录全部更新.
if (mongo.TryConnect()) mongo.Disconnect();
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="selector"></param>
public static void Delete<T>(string dataBaseName, string collectionName, Expression<Func<T, bool>> selector)
where T : class
{
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
collection.Remove(selector, true);
if (mongo.TryConnect()) mongo.Disconnect();
}
}
/// <summary>
/// Linq方式获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static List<T> GetListLinq<T>(int pageIndex, int pageSize, string dataBaseName, string collectionName, Expression<Func<T, bool>> selector)
where T : class
{
List<T> reslut = null;
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
reslut = collection.Linq().Where(selector).
Skip(pageSize * (pageIndex - 1)).
Take(pageSize).ToList();
if (mongo.TryConnect()) mongo.Disconnect();
}
return reslut;
}
/// <summary>
/// Document方式获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="dataBaseName"></param>
/// <param name="collectionName"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static List<T> GetListDocument<T>(int pageIndex, int pageSize, string dataBaseName, string collectionName, object selector)
where T : class
{
List<T> reslut = null;
using (IMongo mongo = new Mongo(MongoConfigurationBuilerMapping<T>().BuildConfiguration()))
{
mongo.Connect();
IMongoDatabase dataBase = mongo.GetDatabase(dataBaseName);
IMongoCollection<T> collection = dataBase.GetCollection<T>(collectionName);
reslut = collection.Find(selector)
.Skip(pageSize * (pageIndex - 1))
.Limit(pageSize).Documents.ToList();
if (mongo.TryConnect()) mongo.Disconnect();
}
return reslut;
}
}
}