1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Configuration;
6 using System.Linq.Expressions;
7 8 using MongoDB.Configuration;
9 using MongoDB.Linq;
10 using MongoDB.Attributes;
11 12 13 namespace MongoDB.Test
14 {
15 public class MongodbHelper
where T : class
16 {
17 string connectionString = string.Empty;
18
19 string databaseName = string.Empty;
20
21 string collectionName = string.Empty;
22
23 static MongodbHelper mongodb;
24
25 #region 初始化操作
26 ///
27 /// 初始化操作
28 ///
29 public MongodbHelper()
30 {
31 connectionString = "Server=127.0.0.1:2222";
32 databaseName = "shopex";
33 collectionName = "person";
34 }
35 #endregion
36
37 #region 实现linq查询的映射配置
38 ///
39 /// 实现linq查询的映射配置
40 ///
41 public MongoConfiguration configuration
42 {
43 get
44 {
45 var config = new MongoConfigurationBuilder();
46
47 config.Mapping(mapping =>
48 {
49 mapping.DefaultProfile(profile =>
50 {
51 profile.SubClassesAre(t => t.IsSubclassOf(typeof(T)));
52 });
53 mapping.Map();
54 mapping.Map();
55 });
56
57 config.ConnectionString(connectionString);
58
59 return config.BuildConfiguration();
60 }
61 }
62 #endregion
63
64 #region 插入操作
65 ///
66 /// 插入操作
67 ///
68 ///
69 ///
70 public void Insert(T t)
71 {
72 using (Mongo mongo = new Mongo(configuration))
73 {
74 try
75 {
76 mongo.Connect();
77
78 var db = mongo.GetDatabase(databaseName);
79
80 var collection = db.GetCollection(collectionName);
81
82 collection.Insert(t, true);
83
84 mongo.Disconnect();
85
86 }
87 catch (Exception)
88 {
89 mongo.Disconnect();
90 throw;
91 }
92 }
93 }
94 #endregion
95
96 #region 更新操作
97 ///
98 /// 更新操作
99 ///
100 ///
101 ///
102 public void Update(T t, Expressionbool>> func)
103 {
104 using (Mongo mongo = new Mongo(configuration))
105 {
106 try
107 {
108 mongo.Connect();
109
110 var db = mongo.GetDatabase(databaseName);
111
112 var collection = db.GetCollection(collectionName);
113
114 collection.Update(t, func, true);
115
116 mongo.Disconnect();
117
118 }
119 catch (Exception)
120 {
121 mongo.Disconnect();
122 throw;
123 }
124 }
125 }
126 #endregion
127
128 #region 获取集合
129 ///
130 ///获取集合
131 ///
132 ///
133 ///
134 public List List(int pageIndex, int pageSize, Expressionbool>> func, out int pageCount)
135 {
136 pageCount = 0;
137
138 using (Mongo mongo = new Mongo(configuration))
139 {
140 try
141 {
142 mongo.Connect();
143
144 var db = mongo.GetDatabase(databaseName);
145
146 var collection = db.GetCollection(collectionName);
147
148 pageCount = Convert.ToInt32(collection.Count());
149
150 var personList = collection.Linq().Where(func).Skip(pageSize * (pageIndex - 1))
151 .Take(pageSize).Select(i => i).ToList();
152
153 mongo.Disconnect();
154
155 return personList;
156
157 }
158 catch (Exception)
159 {
160 mongo.Disconnect();
161 throw;
162 }
163 }
164 }
165 #endregion
166
167 #region 读取单条记录
168 ///
169 ///读取单条记录
170 ///
171 ///
172 ///
173 public T Single(Expressionbool>> func)
174 {
175 using (Mongo mongo = new Mongo(configuration))
176 {
177 try
178 {
179 mongo.Connect();
180
181 var db = mongo.GetDatabase(databaseName);
182
183 var collection = db.GetCollection(collectionName);
184
185 var single = collection.Linq().FirstOrDefault(func);
186
187 mongo.Disconnect();
188
189 return single;
190
191 }
192 catch (Exception)
193 {
194 mongo.Disconnect();
195 throw;
196 }
197 }
198 }
199 #endregion
200
201 #region 删除操作
202 ///
203 /// 删除操作
204 ///
205 ///
206 ///
207 public void Delete(Expressionbool>> func)
208 {
209 using (Mongo mongo = new Mongo(configuration))
210 {
211 try
212 {
213 mongo.Connect();
214
215 var db = mongo.GetDatabase(databaseName);
216
217 var collection = db.GetCollection(collectionName);
218
219 //这个地方要注意,一定要加上T参数,否则会当作object类型处理
220 //导致删除失败
221 collection.Remove(func);
222
223 mongo.Disconnect();
224
225 }
226 catch (Exception)
227 {
228 mongo.Disconnect();
229 throw;
230 }
231 }
232 }
233 #endregion
234 }
235
236 #region 数据实体
237 ///
238 /// 数据实体
239 ///
240 public class Person
241 {
242 [MongoAlias("_id")]
243 public string ID { get; set; }
244
245 public string Name { get; set; }
246
247 public int Age { get; set; }
248
249 public DateTime CreateTime { get; set; }
250 }
251 #endregion
252 }