由於須要,寫了一個基於泛型的helper,這樣要使用起來方便一點。html
爲了你們也不重複造輪子,因此發出來但願能幫到誰。mysql
複雜的查詢最好用linq,這也是mongodb官方建議的。git
這部分不少文章都提到了,須要注意的是用的驅動與你的mongodb版本還有你.Net好像有點關係github
我是mongodb-2.x,.NET4,driver我用的是1.x系列sql
2.x系列好像我這種配置用不起,你們能夠試一試,貌似要.NET要4.5才行mongodb
驅動下載地址:數據庫
https://github.com/mongodb/mongo-csharp-driverc#
這裏有個小坑,mongodb的數據庫鏈接字符串和mysql是不同的,不少文章沒有提到完整的鏈接字符串,花半天在官網上看到了函數
mongodb://username:password@myserver:port/databaseName
其餘沒什麼,但請注意ID、時間的類型,用的是mongdoDB本身的數據類型post
這裏用了一個虛函數,是爲了方便helper裏面用泛型獲取id
如下是Model的源碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB.Driver; using MongoDB.Bson; namespace WindowsFormsApplication1.Model { public abstract class MongoModel { public ObjectId id { get; set; } public BsonDateTime created_at { get; set; } public BsonDateTime updated_at { get; set; } } public class AccountModel : MongoModel {
//例子 public AccountModel() { } public string name { get; set; } } }
由於mongodb的操做語句必須大量用到你的Model,所以考慮用泛型來作Helper
用Builder模式的緣由無非是以爲好玩,你能夠修改代碼用構造函數直接初始化
我也沒有用靜態方法,你有須要能夠本身修改
如下是helper的源碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization; using MongoDB.Driver; using MongoDB.Bson; using MongoDB.Driver.Builders; namespace FrameWork { public class MongoHelper<T> where T : WindowsFormsApplication1.Model.MongoModel { public string conn; public string dbName; public string collectionName; private MongoCollection<T> collection; private MongoHelper() { } /// <summary> /// 設置你的collection /// </summary> public void SetCollection() { MongoClient client = new MongoClient(conn); var server = client.GetServer(); var database = server.GetDatabase(dbName); collection = database.GetCollection<T>(collectionName); } /// <summary> /// 你用linq的時候會用到 /// </summary> public void getCollection() { MongoClient client = new MongoClient(conn); var server = client.GetServer(); var database = server.GetDatabase(dbName); collection = database.GetCollection<T>(collectionName); } /// <summary> /// 查找 /// </summary> /// <param name="query"></param> /// <returns></returns> public T Find(IMongoQuery query) { return this.collection.FindOne(query); } /** * 條件查詢用linq * http://mongodb.github.io/mongo-csharp-driver/1.11/linq/ * */ public List<T> FindAll() { return this.collection.FindAll().ToList(); } /// <summary> /// 修改 /// </summary> /// <param name="model"></param> /// <returns></returns> public long Update(T model) { BsonDocument doc = BsonExtensionMethods.ToBsonDocument(model); WriteConcernResult res = this.collection.Update(Query.EQ("_id", model.id), new UpdateDocument(doc)); return res.DocumentsAffected; } /// <summary> /// 添加 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Insert(T model) { WriteConcernResult res = this.collection.Insert(model); return res.Ok; } /// <summary> /// 刪除 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool Delete(T model) { WriteConcernResult res = this.collection.Remove(Query.EQ("_id", model.id)); return res.Ok; } /// <summary> /// 構造器 /// </summary> /// <typeparam name="T"></typeparam> public class Builder<T> where T : WindowsFormsApplication1.Model.MongoModel { private MongoHelper<T> client; public Builder() { client = new MongoHelper<T>(); } public void setConn(string conn) { client.conn = conn; } public void setDbName(string dbName) { client.dbName = dbName; } public void setCollectionName(string collectionName) { client.collectionName = collectionName; } public MongoHelper<T> build() { client.SetCollection(); return client; } } } }
很簡單,我寫在demo的form代碼裏了,註釋也寫的很清楚什麼流程
1.設計好你的model
2.初始化數據庫配置
3.build一個helper
4.調用方法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using System.IO; using FrameWork; namespace WindowsFormsApplication1 { /** * * MongoDB數據庫增刪改查DEMO * 任意拷貝、修改 * 僅供學習 * 曾維周 16/2/25 * * App獨立開發羣 533838427 * * */ public partial class MainForm : DevComponents.DotNetBar.Metro.MetroForm { public Model.ConfModel conf = new Model.ConfModel(); private bool isFirst = true; private string filePath; private List<Model.AccountModel> accounts = new List<Model.AccountModel>(); private FrameWork.MongoHelper<Model.AccountModel> client; public MainForm() { InitializeComponent(); this.Activated += new EventHandler(Form2_Activated); } void Form2_Activated(object sender, EventArgs e) { if (isFirst) { init(); isFirst = false; } } void init() { /** * * step-1 * 配置你的mongodb連接 * 請配置完 * * */ conf.mongodb_dbAddr = "localhost"; } private void buttonX2_Click(object sender, EventArgs e) { /** * * step-2 * 請操做前修改好你的model * * step-3 * 用builder初始化一個helper * 固然你也徹底能夠修改代碼直接在構造函數裏面初始化 * 我是以爲好玩 * * */ FrameWork.MongoHelper<Model.AccountModel>.Builder<Model.AccountModel> builder = new FrameWork.MongoHelper<Model.AccountModel>.Builder<Model.AccountModel>(); builder.setCollectionName("你的collection名字"); builder.setConn(conf.mongodb_conn); builder.setDbName(conf.mongodb_dbName); client = builder.build(); } private void buttonX1_Click(object sender, EventArgs e) { //增 Model.AccountModel account = new Model.AccountModel(); account.name = "love"; client.Insert(account); //刪 client.Delete(account); //改 account.name = "not love"; client.Update(account); //查 Model.AccountModel res = client.Find(MongoDB.Driver.Builders.Query<Model.AccountModel>.EQ(xx => xx.id, account.id)); //強烈建議用linq進行查詢操做 //http://mongodb.github.io/mongo-csharp-driver/1.11/linq/ //var query = collection.AsQueryable<Model.AccountModel>().Where(e => e.FirstName == "John"); } } }
http://mongodb.github.io/mongo-csharp-driver/1.11/linq/
http://blog.csdn.net/haukwong/article/details/7840158
http://www.cnblogs.com/viprx/archive/2012/09/07/2674637.html
連接: http://pan.baidu.com/s/1qX3vfdE 密碼: buh2