|
34 /// 数据类型
135 /// 数据库连接串
136 /// 数据库名称
137 /// 集合名称
138 /// 数据对象集合
139 public static List GetAll(string connectionString, string dbName, string collectionName)
140 where T : EntityBase
141 {
142 var db = GetDatabase(connectionString, dbName);
143 var collection = db.GetCollection(collectionName);
144 return collection.FindAll().ToList();
145 }
146
147 #endregion
148
149 #region 删除
150
151 ///
152 /// 删除集合中符合条件的数据
153 ///
154 /// 数据库连接串
155 /// 数据库名称
156 /// 集合名称
157 /// 查询条件
158 public static void DeleteByCondition(string connectionString, string dbName, string collectionName, IMongoQuery query)
159 {
160 var db = GetDatabase(connectionString, dbName);
161 var collection = db.GetCollection(collectionName);
162 collection.Remove(query);
163 }
164
165 ///
166 /// 删除集合中的所有数据
167 ///
168 /// 数据库连接串
169 /// 数据库名称
170 /// 集合名称
171 public static void DeleteAll(string connectionString, string dbName, string collectionName)
172 {
173 var db = GetDatabase(connectionString, dbName);
174 var collection = db.GetCollection(collectionName);
175 collection.RemoveAll();
176 }
177
178 #endregion
179
180 }
181 }
?
3、编写测试类
?
(1)配置数据库参数
?
在配置文件中编写数据库连接串和数据库名称。
将其写入C#代码中:
using System.Configuration;
namespace MongoDbTest
{
///
/// 数据库配置参数
///
internal static class DbConfigParams
{
private static string _conntionString = ConfigurationManager.AppSettings["MongoDBConn"];
///
/// 获取 数据库连接串
///
public static string ConntionString
{
get { return _conntionString; }
}
private static string _dbName = ConfigurationManager.AppSettings["MongoDBName"];
///
/// 获取 数据库名称
///
public static string DbName
{
get { return _dbName; }
}
}
}
?
另外,将集合名称写到C#代码中作为字符串常量:
namespace MongoDbTest
{
public class CollectionNames
{
public const string User = "User";
public const string Student = "Student";
}
}
?
(2)编写实体类
?
首先,编写实体基类,其中含有默认的Id:
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDbTest.Models
{
///
/// 实体基类
///
public class EntityBase
{
///
/// 主键
///
[BsonId]
public ObjectId Id { get; set; }
}
}
?
然后,编写实体类:
namespace MongoDbTest.Models
{
///
/// 学生类
///
public class Student : EntityBase
{
///
/// 获取 姓名
///
public string Name { get; set; }
///
/// 获取 年龄
///
public int Age { get; set; }
///
/// 获取 状态
///
public State State { get; set; }
}
}
?
其中,State枚举类定义如下:
namespace MongoDbTest.Models
{
///
/// 状态
///
public enum State
{
///
/// 全部
///
All = 0,
///
/// 正常
///
Normal = 1,
///
/// 未使用
///
Unused = 2,
}
}
?
(3)编写测试代码
?
在主程序中编写测试代码:
1 using System;
2 using System.Collections.Generic;
3 using MongoDB.Bson;
4 using MongoDB.Driver.Builders;
5 using MongoDbTest.Models;
6
7 namespace MongoDbTest
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Console.Title = "Mongo DB Test";
14 InsertTest();
15 //QueryTest();
16 //UpdateTest();
17 DeleteTest();
18
19 Console.WriteLine("Finish!");
20
21 Console.ReadLine();
22 }
23
24 ///
25 /// 插入数据测试
26 ///
27 static void InsertTest()
28 {
29 var random = new Random();
30 for (var i = 1; i <= 10; i++)
31 {
32 var item = new Student()
33 {
34 Name = "我的名字" + i,
35 Age = random.Next(25, 30),
36 State = i%2 == 0 ? State.Normal : State.Unused
37 };
38 MongoDbHepler.Insert(DbConfigParams.ConntionString, DbConfigParams.DbName, CollectionNames.Student, item);
39 }
40 }
41
42 ///
43 /// 查询测试
44 ///
|