using System;
02.
using System.Collections.Generic;
03.
using System.Linq;
04.
using System.Text;
05.
using MongoDB.Driver;
06.
using MongoDB.Bson;
07.
namespace WindowsFormsApplication1.Model
08.
{
09.
public
abstract
class
MongoModel
10.
{
11.
public
ObjectId id { get; set; }
12.
public
BsonDateTime created_at { get; set; }
13.
public
BsonDateTime updated_at { get; set; }
14.
}
15.
16.
public
class
AccountModel : MongoModel
17.
{
18.
//例子
19.
public
AccountModel()
20.
{
21.
}
22.
23.
public
string name { get; set; }
24.
25.
}
26.
}
Helper的编写
因为mongodb的操作语句必须大量用到你的Model,因此考虑用泛型来做Helper
用Builder模式的原因无非是觉得好玩,你可以修改代码用构造函数直接初始化
我也没有用静态方法,你有需要可以自己修改
以下是helper的源码
001.
using System;
002.
using System.Collections.Generic;
003.
using System.Linq;
004.
using System.Text;
005.
using System.IO;
006.
using System.Xml;
007.
using System.Xml.Serialization;
008.
using MongoDB.Driver;
009.
using MongoDB.Bson;
010.
using MongoDB.Driver.Builders;
011.
012.
namespace FrameWork
013.
{
014.
public
class
MongoHelper<T> where T : WindowsFormsApplication1.Model.MongoModel
015.
{
016.
public
string conn;
017.
public
string dbName;
018.
public
string collectionName;
019.
020.
private
MongoCollection<T> collection;
021.
022.
private
MongoHelper()
023.
{
024.
025.
}
026.
027.
/// <summary>
028.
/// 设置你的collection
029.
/// </summary>
030.
public
void
SetCollection()
031.
{
032.
MongoClient client =
new
MongoClient(conn);
033.
var server = client.GetServer();
034.
var database = server.GetDatabase(dbName);
035.
collection = database.GetCollection<T>(collectionName);
036.
}
037.
038.
/// <summary>
039.
/// 你用linq的时候会用到
040.
/// </summary>
041.
public
void
getCollection()
042.
{
043.
MongoClient client =
new
MongoClient(conn);
044.
var server = client.GetServer();
045.
var database = server.GetDatabase(dbName);
046.
collection = database.GetCollection<T>(collectionName);
047.
}
048.
049.
/// <summary>
050.
/// 查找
051.
/// </summary>
052.
/// <param name='query'></param>
053.
/// <returns></returns>
054.
public
T Find(IMongoQuery query)
055.
{
056.
return
this
.collection.FindOne(query);
057.
}
058.
059.
/**
060.
* 条件查询用linq
062.
* */
063.
public
List<T> FindAll()
064.
{
065.
return
this
.collection.FindAll().ToList();
066.
}
067.
068.
/// <summary>
069.
/// 修改
070.
/// </summary>
071.
/// <param name='model'></param>
072.
/// <returns></returns>
073.
public
long
Update(T model)
074.
{
075.
BsonDocument doc = BsonExtensionMethods.ToBsonDocument(model);
076.
WriteConcernResult res =
this
.collection.Update(Query.EQ(
'_id'
, model.id),
new
UpdateDocument(doc));
077.
return
res.DocumentsAffected;
078.
}
079.
080.
/// <summary>
081.
/// 添加
082.
/// </summary>
083.
/// <param name='model'></param>
084.
/// <returns></returns>
085.
public
bool Insert(T model)
086.
{
087.
WriteConcernResult res =
this
.collection.Insert(model);
088.
return
res.Ok;
089.
}
090.
091.
/// <summary>
092.
/// 删除
093.
/// </summary>
094.
/// <param name='model'></param>
095.
/// <returns></returns>
096.
public
bool Delete(T model)
097.
{
098.
WriteConcernResult res =
this
.collection.Remove(Query.EQ(
'_id'
, model.id));
099.
return
res.Ok;
100.
}
101.
102.
/// <summary>
103.
/// 构造器
104.
/// </summary>
105.
/// <typeparam name='T'></typeparam>
106.
public
class
Builder<T> where T : WindowsFormsApplication1.Model.MongoModel
107.
{
108.
private
MongoHelper<T> client;
109.
110.
public
Builder()
111.
{
112.
client =
new
MongoHelper<T>();
113.
}
114.
115.
public
void
setConn(string conn)
116.
{
117.
client.conn = conn;
118.
}
119.
120.
public
void
setDbName(string dbName)
121.
{
122.
client.dbName = dbName;
123.
}
124.
125.
public
void
setCollectionName(string collectionName)
126.
{
127.
client.collectionName = collectionName;
128.
}
129.
130.
public
MongoHelper<T> build()
131.
{
132.
client.SetCollection();
133.
return
client;
134.
}
135.
}
136.
}
137.
}
Helper的使用
很简单,我写在demo的form代码里了,注释也写的很清楚什么流程
1.设计好你的model
2.初始化数据库配置
3.build一个helper
4.调用方法
001.
using System;
002.
using System.Collections.Generic;
003.
using System.ComponentModel;
004.
using System.Data;
005.
using System.Drawing;
006.
using System.Text;
007.
using System.Windows.Forms;
008.
using DevComponents.DotNetBar;
009.
using System.IO;
010.
using FrameWork;
011.
012.
namespace WindowsFormsApplication1
013.
{
014.
/**
015.
*
016.
* MongoDB数据库增删改查DEMO
017.
* 任意拷贝、修改
018.
* 仅供学习
019.
* 曾维周 16/2/25
020.
*
021.
* App独立开发群 533838427
022.
*
023.
* */
024.
public
partial
class
MainForm : DevComponents.DotNetBar.Metro.MetroForm
025.
{
026.
public
Model.ConfModel conf =
new
Model.ConfModel();
027.
private
bool isFirst =
true
;
028.
private
string filePath;
029.
private
List<Model.AccountModel> accounts =
new
List<Model.AccountModel>();
030.
private
FrameWork.MongoHelper<Model.AccountModel> client;
031.
public
MainForm()
032.
{
033.
InitializeComponent();
034.
this
.Activated +=
new
EventHandler(Form2_Activated);
035.
}
036.
037.
void
Form2_Activated(object sender, EventArgs e)
038.
{
039.
if
(isFirst)
040.
{
041.
init();
042.
isFirst =
false
;
043.
}
044.
}
045.
046.
void
init()
047.
{
048.
/**
049.
*
050.
* step-1
051.
* 配置你的mongodb链接
052.
* 请配置完
053.
*
054.
* */
055.
conf.mongodb_dbAddr =
'localhost'
;
056.
}
057.
058.
private
void
buttonX2_Click(object sender, EventArgs e)
059.
{
060.
/**
061.
*
062.
* step-2
063.
* 请操作前修改好你的model
064.
*
065.
* step-3
066.
* 用builder初始化一个helper
067.
* 当然你也完全可以修改代码直接在构造函数里面初始化
068.
* 我是觉得好玩
069.
*
070.
* */
071.
FrameWork.MongoHelper<Model.AccountModel>.Builder<Model.AccountModel> builder =
new
FrameWork.MongoHelper<Model.AccountModel>.Builder<Model.AccountModel>();
072.
builder.setCollectionName(
'你的collection名字'
);
073.
builder.setConn(conf.mongodb_conn);
074.
builder.setDbName(conf.mongodb_dbName);
075.
client = builder.build();
076.
}
077.
078.
private
void
buttonX1_Click(object sender, EventArgs e)
079.
{
080.
//增
081.
Model.AccountModel account =
new
Model.AccountModel();
082.
account.name =
'love'
;
083.
client.Insert(account);
084.
085.
//删
086.
client.Delete(account);
087.
088.
//改
089.
account.name =
'not love'
;
090.
client.Update(account);
091.
092.
//查
093.
Model.AccountModel res = client.Find(MongoDB.Driver.Builders.Query<Model.AccountModel>.EQ(xx => xx.id, account.id));
094.
095.
//强烈建议用linq进行查询操作
097.
//var query = collection.AsQueryable<Model.AccountModel>().Where(e => e.FirstName == 'John');
098.
099.
}
100.
101.
}
102.
}