本文利用MongoDB官方发布的C#驱动,封闭了对MongoDB数据库的增删改查访问方法。
?
1、引用MongoDB for C# Driver
?
从网上
下载C#访问MongoDB的驱动,得到两个DLL:
?
MongoDB.Driver.dll
MongoDB.Bson.dll
?
将它们引用到项目中。
?
?
编写MongoDB访问帮助类MongoDbHelper:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using MongoDB.Bson;
5 using MongoDB.Driver;
6 using MongoDB.Driver.Builders;
7 using MongoDbTest.Models;
8
9 namespace MongoDbTest
10 {
11 ///
12 /// MongoDB帮助类
13 ///
14 internal static class MongoDbHepler
15 {
16 ///
17 /// 获取数据库实例对象
18 ///
19 /// 数据库连接串
20 /// 数据库名称
21 /// 数据库实例对象
22 private static MongoDatabase GetDatabase(string connectionString, string dbName)
23 {
24 var server = MongoServer.Create(connectionString);
25 return server.GetDatabase(dbName);
26 }
27
28 #region 新增
29
30 ///
31 /// 插入一条记录
32 ///
33 /// 数据类型
34 /// 数据库连接串
35 /// 数据名称
36 /// 集合名称
37 /// 数据对象
38 public static void Insert(string connectionString, string dbName, string collectionName, T model) where T : EntityBase
39 {
40 if (model == null)
41 {
42 throw new ArgumentNullException("model", "待插入数据不能为空");
43 }
44 var db = GetDatabase(connectionString, dbName);
45 var collection = db.GetCollection(collectionName);
46 collection.Insert(model);
47 }
48
49 #endregion
50
51 #region 更新
52
53 ///
54 /// 更新数据
55 ///
56 /// 数据库连接串
57 /// 数据库名称
58 /// 集合名称
59 /// 查询条件
60 /// 更新字段
61 public static void Update(string connectionString, string dbName, string collectionName, IMongoQuery query, Dictionary dictUpdate)
62 {
63 var db = GetDatabase(connectionString, dbName);
64 var collection = db.GetCollection(collectionName);
65 var update = new UpdateBuilder();
66 if (dictUpdate != null && dictUpdate.Count > 0)
67 {
68 foreach (var item in dictUpdate)
69 {
70 update.Set(item.Key, item.Value);
71 }
72 }
73 var d = collection.Update(query, update, UpdateFlags.Multi);
74 }
75
76 #endregion
77
78 #region 查询
79
80 ///
81 /// 根据ID获取数据对象
82 ///
83 /// 数据类型
84 /// 数据库连接串
85 /// 数据库名称
86 /// 集合名称
87 /// ID
88 /// 数据对象
89 public static T GetById(string connectionString, string dbName, string collectionName, ObjectId id)
90 where T : EntityBase
91 {
92 var db = GetDatabase(connectionString, dbName);
93 var collection = db.GetCollection(collectionName);
94 return collection.FindOneById(id);
95 }
96
97 ///
98 /// 根据查询条件获取一条数据
99 ///
100 /// 数据类型
101 /// 数据库连接串
102 /// 数据库名称
103 /// 集合名称
104 /// 查询条件
105 /// 数据对象
106 public static T GetOneByCondition(string connectionString, string dbName, string collectionName, IMongoQuery query)
107 where T : EntityBase
108 {
109 var db = GetDatabase(connectionString, dbName);
110 var collection = db.GetCollection(collectionName);
111 return collection.FindOne(query);
112 }
113
114 ///
115 /// 根据查询条件获取多条数据
116 ///
117 /// 数据类型
118 /// 数据库连接串
119 /// 数据库名称
120 /// 集合名称
121 /// 查询条件
122 /// 数据对象集合
123 public static List GetManyByCondition(string connectionString, string dbName, string collectionName, IMongoQuery query)
124 where T : EntityBase
125 {
126 var db = GetDatabase(connectionString, dbName);
127 var collection = db.GetCollection(collectionName);
128 return collection.Find(query).ToList();
129 }
130
131 ///
132 /// 根据集合中的所有数据
133 ///
1