MongoDBHelper

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);

时间: 2024-10-26 21:01:19

MongoDBHelper的相关文章

c#驱动操作mongodb辅助类MongoDBHelper

using MongoDB.Bson; using MongoDB.Driver; using System; using System.Collections.Generic; /* https://docs.mongodb.com/manual/tutorial/update-documents/ https://docs.mongodb.com/manual/tutorial/perform-two-phase-commits/ https://docs.mongodb.com/manua

MongoDB 学习 第八节 驱动实践

作为系列的最后一篇,得要说说C#驱动对mongodb的操作,目前驱动有两种:官方驱动和samus驱动,不过我个人还是喜欢后者, 因为提供了丰富的linq操作,相当方便. 官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads.下载后,还提供了一个酷似msdn的帮助文档. samus驱动:https://github.com/samus/mongodb-csharp/downloads. 下面就具体看看samus驱动,https:

双十一来了,别让你的mongodb宕机了

好久没过来吹牛了,前段时间一直赶项目,没有时间来更新博客,项目也终于赶完了,接下来就要面临双十一这场惊心动魄的处女秀考验, 我们项目中会有一个wcf集群,而集群地址则放在mongodb中,所以mongodb的核心地位可想而知,如果mongodb挂掉,整个项目也就陷入 瘫痪,想让mongodb不宕机,最简单的方法就是要做双机热备,跟传统的关系型数据库的双机热备模式一样,一个主服务器,一个备份服务器, 一个仲裁服务器.如果热备集群中的主服务器宕掉,会有仲裁服务器参与投票来选出一台作为主服务器,我想这

NET下RabbitMQ实践[实战篇]

之前的文章中,介绍了如何将RabbitMQ以WCF方式进行发布.今天就介绍一下我们产品中如何使用RabbitMQ的!          在Discuz!NT企业版中,提供了对HTTP错误日志的记录功能,这一点对企业版非常重要,另外存储错误日志使用了MongoDB,理由很简单,MongoDB的添加操作飞快,即使数量过亿之后插入速度依旧不减.          在开始正文之前,先说明一下本文的代码分析顺序,即:程序入口==>RabbitMQ客户端===>RabbitMQ服务端.好了,闲话少说,开始

C# mongodb帮助类

这是在C#连接MongoDB的帮助类,所使用的驱动是在Vs2015的Nuget管理器中下载的mongodb驱动. 下载第一个,会自动下载下面的两个,不要删除. 在配置文件中配置连接字符串connStr和数据库名称dbName: 1 <appSettings> 2 <add key="webpages:Version" value="3.0.0.0"/> 3 <add key="webpages:Enabled" va

mongodb c# driver(驱动)介绍及CURD

mongodb c# driver(驱动)介绍 目前基于C#的mongodb驱动有两种,分别是官方驱动(下载地址)和samus驱动(下载地址). 本次我们只演示官方驱动的使用方法. 官方驱动文档查看 第一步:引用驱动dll 引用驱动有两种方式: 1. 根据上面的下载地址下载对应的版本,然后引用到项目中. 2. 在项目的引用上右击->管理NuGet程序包(首先确保安装了nuget扩展包)->联机搜索官方驱动dll(搜索条件是 “Official MongoDB”)->安装成功后会引用3个d

MongoHelper

做项目时需要用到MongoDb,以下代码略做参考,转自:http://www.cnblogs.com/yanc/archive/2011/06/28/2091975.html 我只摘了一个方法,其他没有测试... using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB.Driver; using MongoDB.Bson; using MongoDB.D

MongoDB实战

第一节:环境配置及基础操作 一: 下载 上MongoDB官网 ,我们发现有32bit和64bit,这个就要看你系统了,不过这里有两点注意: ①:根据业界规则,偶数为"稳定版"(如:1.6.X,1.8.X),奇数为"开发版"(如:1.7.X,1.9.X),这两个版本的区别相信大家都知道吧. ②:32bit的mongodb最大只能存放2G的数据,64bit就没有限制. 我这里就下载"2.0.2版本,32bit",ok,下载之后我就放到"E盘

C#操作MongoDB数据库方法

本文利用MongoDB官方发布的C#驱动,封闭了对MongoDB数据库的增删改查访问方法. 1.引用MongoDB for C# Driver 从网上下载C#访问MongoDB的驱动,得到两个DLL: MongoDB.Driver.dll MongoDB.Bson.dll 将它们引用到项目中. 2.编写数据库访问帮助类 编写MongoDB访问帮助类MongoDbHelper: 1 using System; 2 using System.Collections.Generic; 3 using