您现在的位置:首页 >> Asp.NET >> 内容

C#操作MongoDB帮助类

时间:2015-10-8 11:56:24 点击:

  核心提示:利用MongoDB for C# Driver编写访问MongoDB数据库的帮助类。1、创建数据库连接?1234567891011/// summary/// 获取数据库实例对象/// /summar...
利用MongoDB for C# Driver编写访问MongoDB数据库的帮助类。
 
1、创建数据库连接
 
?
1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// 获取数据库实例对象
/// </summary>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <returns>数据库实例对象</returns>
private static MongoDatabase GetDatabase(string connectionString, string dbName)
{
var server = MongoServer.Create(connectionString);
return server.GetDatabase(dbName);
}

 

 
2、新增数据
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <summary>
/// 插入一条记录
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="model">数据对象</param>
public static void Insert<T>(string connectionString, string dbName, string collectionName, T model) where T : EntityBase
{
if (model == null)
{
throw new ArgumentNullException("model", "待插入数据不能为空");
}
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
collection.Insert(model);
}

 

3、更新数据
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// <summary>
/// 更新数据
/// </summary>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
/// <param name="dictUpdate">更新字段</param>
public static void Update(string connectionString, string dbName, string collectionName, IMongoQuery query,
Dictionary<string, BsonValue> dictUpdate)
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection(collectionName);
var update = new UpdateBuilder();
if (dictUpdate != null && dictUpdate.Count > 0)
{
foreach (var item in dictUpdate)
{
update.Set(item.Key, item.Value);
}
}
collection.Update(query, update, UpdateFlags.Multi);
}
 
/// <summary>
/// 更新数据
/// </summary>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
/// <param name="update">更新条件</param>
public static void Update(string connectionString, string dbName, string collectionName, IMongoQuery query,
IMongoUpdate update)
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection(collectionName);
collection.Update(query, update, UpdateFlags.Multi);
}

 

4、查询数据
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/// <summary>
/// 根据ID获取数据对象
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="id">ID</param>
/// <returns>数据对象</returns>
public static T GetById<T>(string connectionString, string dbName, string collectionName, string id)
where T : EntityBase
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
var tId = new ObjectId(id);
return collection.FindOneById(tId);
}
 
/// <summary>
/// 根据查询条件获取一条数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
/// <returns>数据对象</returns>
public static T GetOneByCondition<T>(string connectionString, string dbName,
string collectionName, IMongoQuery query)
where T : EntityBase
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
return collection.FindOne(query);
}
 
/// <summary>
/// 根据查询条件获取多条数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
/// <returns>数据对象集合</returns>
public static List<T> GetManyByCondition<T>(string connectionString, string dbName,
string collectionName, IMongoQuery query)
where T : EntityBase
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
return collection.Find(query).ToList();
}
 
/// <summary>
/// 根据查询条件获取多条数据(分页)
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
/// <param name="sortBy">排序</param>
/// <param name="pageIndex">页号</param>
/// <param name="pageSize">每页大小</param>
/// <returns>分页数据集合</returns>
public static PagingList<T> GetManyByPageCondition<T>(string connectionString, string dbName,
string collectionName, IMongoQuery query, IMongoSortBy sortBy, int pageIndex, int pageSize)
where T : EntityBase
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
var result = new PagingList<T>();
result.Total = (int)collection.Count(query);
result.CurrentPageList =
collection.Find(query).SetSortOrder(sortBy).SetSkip((pageIndex - 1)*pageSize).SetLimit(pageSize).ToList();
return result;
}
 
/// <summary>
/// 获取集合中的所有数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <returns>数据对象集合</returns>
public static List<T> GetAll<T>(string connectionString, string dbName, string collectionName)
where T : EntityBase
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection<T>(collectionName);
return collection.FindAll().ToList();
}

 

5、删除数据
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <summary>
/// 删除集合中符合条件的数据
/// </summary>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
/// <param name="query">查询条件</param>
public static void DeleteByCondition(string connectionString, string dbName,
string collectionName, IMongoQuery query)
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection(collectionName);
collection.Remove(query);
}
 
/// <summary>
/// 删除集合中的所有数据
/// </summary>
/// <param name="connectionString">数据库连接串</param>
/// <param name="dbName">数据库名称</param>
/// <param name="collectionName">集合名称</param>
public static void DeleteAll(string connectionString, string dbName, string collectionName)
{
var db = GetDatabase(connectionString, dbName);
var collection = db.GetCollection(collectionName);
collection.RemoveAll();
}

 

 

作者:不详 来源:网络
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 青春网络编程技术(www.qcfupo.com) © 2020 版权所有 All Rights Reserved.
  • Powered by qcfupo! V3.0sp1
  • 百度友情链接: 百家乐[www.chinawestnews.net/aomenbaijiale/] 赌博网[www.chinawestnews.net/amdbwz/] 海立方[www.bjl.gov.cn/bjgylc/] 娱乐城[www.gtxrmzf.gov.cn/mapai/] 娱乐城[www.hfsf.gov.cn/yfgj/] 威尼斯人[www.hllinkou.gov.cn/baijile/] 澳门威尼斯[www.jjwgj.gov.cn/wnsr/] 888真人[www.lagm.gov.cn/888zhenren/] 百乐坊[www.laoling.gov.cn/blfylc/] 现金网[www.longyu.gov.cn/xjw/] 百家乐[www.chinawestnews.net/ambjl/] 新葡京娱乐场[www.sjzedu.gov.cn/jsylc/] 时时彩[www.snjw.gov.cn/ssc/] 赌博[www.chinawestnews.net/aomendubo/]