public class MongoDbHelper
{
public static readonly string
connectionString =
ConfigurationManager.AppSettings["MongoDBConnectionString"];
public
static readonly string database =
ConfigurationManager.AppSettings["MongoDBDatabase"];
public static IMongoCollection<T>
GetCollection<T>(string collectionName, out Mongo mongodb) where T :
class
{
Mongo mongo = new Mongo(connectionString);
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T> categories
= db.GetCollection<T>(collectionName);
mongodb = mongo;
return categories;
}
#region 新增
/// <summary>
/// 插入新数据
///
</summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
///
<param name="entiry"></param>
public static bool
InsertOne<T>(string collectionName, T entity) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
categories.Insert(entity, true);
mongo.Disconnect();
}
return true;
}
///
<summary>
/// 插入多个数据
/// </summary>
///
<typeparam name="T"></typeparam>
/// <param
name="collectionName"></param>
/// <param
name="entiry"></param>
public static bool
InsertAll<T>(string collectionName, IEnumerable<T> entity) where T :
class
{
using (Mongo mongo = new
Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
categories.Insert(entity, true);
mongo.Disconnect();
}
return true;
}
#endregion
#region 更新
/// <summary>
/// 更新操作
///
</summary>
/// <typeparam
name="T">类型</typeparam>
/// <param
name="collectionName">表名</param>
/// <param
name="query">条件</param>
/// <param
name="entry">新实体</param>
public static bool
Update<T>(string collectionName, T entity, Document query) where T :
class
{
using (Mongo mongo = new
Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
categories.Update(entity, query, true);
mongo.Disconnect();
}
return true;
}
/// <summary>
/// 更新操作
/// </summary>
/// <typeparam name="T">类型</typeparam>
///
<param name="collectionName">表名</param>
/// <param
name="query">条件</param>
/// <param
name="entry">新实体</param>
public static bool
UpdateAll<T>(string collectionName, Document entity, Document query) where
T : class
{
using (Mongo mongo = new
Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
categories.Update(entity, query, UpdateFlags.MultiUpdate, true);
mongo.Disconnect();
}
return true;
}
#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 db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
result = categories.FindOne(query);
mongo.Disconnect();
}
return result;
}
///
<summary>
/// 根据Id 获取一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param
name="collectionName"></param>
/// <param
name="id"></param>
/// <returns></returns>
public static T GetOneById<T>(string collectionName, string id) where
T : class
{
Document query = new Document("Id",
id);
T result = default(T);
using (Mongo mongo =
new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.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 db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.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>();
using (Mongo mongo
= new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
foreach (T entity in categories.FindAll().Limit(100000).Documents)
{
result.Add(entity);
}
mongo.Disconnect();
}
return result;
}
public
static List<T> GetList<T>(string collectionName, object selector)
where T : class
{
List<T> result = new
List<T>();
using (Mongo mongo = new
Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
foreach (T entity in categories.Find(selector).Documents)
{
result.Add(entity);
}
mongo.Disconnect();
}
return result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param
name="collectionName"></param>
/// <param
name="query"></param>
/// <param
name="Sort"></param>
/// <param
name="cp"></param>
/// <param
name="mp"></param>
/// <returns></returns>
public static List<T> GetList<T>(string collectionName, object
selector, Document sort, int cp, int mp) where T : class
{
List<T> result = new List<T>();
using (Mongo
mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
foreach (T entity in
categories.Find(selector).Sort(sort).Skip((cp - 1) *
mp).Limit(mp).Documents)
{
result.Add(entity);
}
mongo.Disconnect();
}
return result;
}
///
<summary>
/// 获取列表
/// </summary>
///
<typeparam name="T"></typeparam>
/// <param
name="collectionName"></param>
/// <param
name="query"></param>
/// <param
name="Sort"></param>
/// <param
name="cp"></param>
/// <param
name="mp"></param>
/// <returns></returns>
public static List<T> GetList<T>(string collectionName, object
selector, object fields, Document sort, int cp, int mp) where T : class
{
List<T> result = new List<T>();
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
foreach (T entity in categories.Find(selector, fields).Sort(sort).Skip((cp - 1)
* mp).Limit(mp).Documents)
{
result.Add(entity);
}
mongo.Disconnect();
}
return result;
}
/// <summary>
/// 获取符合条件的总记录数
///
</summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
///
<param name="selector"></param>
///
<returns></returns>
public static long
GetTotalCount<T>(string collectionName, object selector) where T :
class
{
using (Mongo mongo = new
Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
long totalCount =
db.GetCollection<T>(collectionName).Count(selector);
mongo.Disconnect();
return totalCount;
}
}
#endregion
#region 删除
/// <summary>
/// 删除数据
///
</summary>
/// <typeparam name="T"></typeparam>
/// <param name="collectionName"></param>
///
<param name="entity"></param>
public static bool
Delete<T>(string collectionName, Document query) where T : class
{
using (Mongo mongo = new Mongo(connectionString))
{
mongo.Connect();
IMongoDatabase db =
mongo.GetDatabase(database);
IMongoCollection<T>
categories = db.GetCollection<T>(collectionName);
categories.Remove(query, true);
mongo.Disconnect();
}
return true;
}
#endregion
}
使用
Document doc = new
Document();//返回的条件集合
//介于某个值中间
Document docLessGreater = new
Document();//参数
docLessGreater.Insert("$gte", startTime,
0);//大于开始时间
docLessGreater.Insert("$lte", endTime,
1);//小于结束时间
doc.Add("CollectTime", docLessGreater);
//in
Document
docIn=new Document("$in", 数组类型);
doc.Add(FieldName,
docIn);
//绝对匹配
doc.Add(FieldName,
ParameterValue);
//模糊匹配
MongoRegex reg = new MongoRegex(".*" +
ParameterValue + ".*", MongoRegexOption.IgnoreCase);
doc.Add(FieldName,
reg);
long totalCount =
MongoDbHelper.GetTotalCount<RssiGpsDto>(MongoTable.RssiGps, doc);
var
list = MongoDbHelper.GetList<RssiGpsDto>(MongoTable.RssiGps, doc, new
Document(), page, rows);