mongodb結合Flexgrid的簡單數據呈現 本示例以常用的:用戶,帖子,評論為基礎模型,實現了一個簡單的MongoDB結合Flexgrid數據呈現的Demo。由于時間所限,功能上僅提供對MongoDB數據的常用查詢操作(分頁,排序,查詢等)。高手請對我這種菜鳥多些包容。 一,
MongoDB結合Flexgrid的簡單數據呈現
?
本示例以常用的:用戶,帖子,評論為基礎模型,實現了一個簡單的MongoDB結合Flexgrid數據呈現的Demo。由于時間所限,功能上僅提供對MongoDB數據的常用查詢操作(分頁,排序,查詢等)。高手請對我這種菜鳥多些包容。
?
一,準備工作:
MongoDB官方下載:
當前最新版本是2.2.0版本。話說,MongoDB的版本更新是相當的快。
?
本示例使用的MongoDB C#版驅動下載:
https://github.com/samus/mongodb-csharp
該驅動附源碼和示例程序,有興趣的朋友可以研究一下,對自己的編碼能力會有很大的提高。用VS打開Source文件夾,編譯后得到程序集,在自己的項目中引用即可。
?
這是官方郵件提供的 C#版驅動:
https://github.com/mongodb/mongo-csharp-driver/downloads
版本很多,而且基本上都是十多兆的東西,然后,沒有然后。
?
如果你不習慣以命令行的方式對MongoDB進行操作和管理。推薦以下客戶端管理工具:
1,MongoVUE。這應該是目前應用最廣的了。
下載地址:
2,虛擬主機,博客園高手自己開發的MongoDB管理工具:
試用了一下,也是相當不錯的!
?
雖然上述管理工具能夠以圖形化的方式與MongoDB交互,但強烈建議你運行mongo.exe客戶端,以命令行的方式對MongoDB進行操作和管理,這會讓你對MongoDB有更加直觀而深刻的體會。另外,Windows8依然內置了DOS。
?
這里有MongoDB的常用命令:
?
這是Flexgrid的官方網站:
頁面頂部有一個碩大的紅色Download按鈕。
?
TestDriven.net,必不可少的開發和測試工具。本示例需要用它向MongoDB中初始化測試數據。下載地址:
?
二,項目結構:
麻雀雖小,五臟俱全。整個項目結構是一個最原始的三層。直接上圖:

三,技術細節:
1,MongoDB數據庫操作。
ADO.NET雖然很強大,但我們通常需要自己動手寫一個SqlHelper。MongoDB也一樣,我們仍有必要在驅動的基礎上對常用的數據操作進行封裝。下面貼出本鳥寫的MongoDB數據庫操作類。大多數方法都與數據查詢相關。貼出主要代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Configuration; using MongoDB; using MongoDB.Linq; using Mcmurphy.Commons.Enumerations; namespace Mcmurphy.DAL { public class MongoDBHelper { #region 基本信息 private static readonly string ConnectionString; private static readonly string DatabaseName; ///
/// 當前Mongo引用 ///
private static Mongo mongo; ///
/// 初始化數據庫配置 ///
static MongoDBHelper() { ConnectionString = ConfigurationManager.AppSettings[“connString”]; DatabaseName = ConfigurationManager.AppSettings[“currentDB”]; } ///
/// 當前Mongo對象 ///
private static Mongo CurrentMongo { get { return new Mongo(ConnectionString); } } ///
/// 當前操作集合 ///
private static IMongoCollection GetCollection() where T:class { try { mongo = CurrentMongo; mongo.Connect(); IMongoDatabase db = GetCurrentDataBase(); IMongoCollection collection = db.GetCollection(); return collection; } catch (Exception ex) { throw ex; } } ///
/// 獲取當前Mongo數據庫對象 ///
/// 當前Mongo數據庫對象 private static IMongoDatabase GetCurrentDataBase() { return mongo.GetDatabase(DatabaseName); } #endregion #region 數據查詢 ///
/// 獲取排序規則 ///
private static IndexOrder GetIndexOrder(DataOrder order) { IndexOrder @orderby = order == DataOrder.Ascending ? IndexOrder.Ascending : IndexOrder.Descending; return orderby; } ///
/// 根據條件進行查詢,并進行分頁 ///
/// 類型參數 /// 條件Lamda表達式 /// 當前頁索引 /// 分頁大小 /// 結果集 public static IEnumerable GetBySearch(System.Linq.Expressions.Expression> selector, int pageIndex, int pageSize) where T : class { try { var currentCollection = GetCollection(); return currentCollection.Find(selector).Skip((pageIndex – 1) * pageSize).Limit(pageSize).Documents.ToList(); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } ///
/// 根據條件進行查詢,并進行分頁和排序 ///
/// 類型參數 /// 條件Lamda表達式 /// 當前頁索引 /// 分頁大小 /// 排序規則 /// 排序字段 /// 結果集 public static IEnumerable GetBySearch(System.Linq.Expressions.Expression> selector, int pageIndex, int pageSize, DataOrder order, string orderField) where T : class { try { IndexOrder orderby = GetIndexOrder(order); var currentCollection = GetCollection(); return currentCollection.Find(selector).Sort(orderField, orderby).Skip((pageIndex – 1) * pageSize).Limit(pageSize).Documents.ToList(); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } ///
/// 根據條件查詢一個對象 ///
/// 類型參數 /// 查詢條件Lamda表達式 /// 查詢對象 public static T GetOneBySearch(System.Linq.Expressions.Expression> selector) where T : class { try { var currentCollection = GetCollection(); return currentCollection.FindOne(selector); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } ///
/// 根據查詢條件獲取總記錄數 ///
/// 類型參數 /// 查詢條件 /// 記錄數 public static long GetTotalCount(System.Linq.Expressions.Expression> selector) where T : class { try { var currentCollection = GetCollection(); return currentCollection.Count(selector); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } ///
/// 獲取總記錄數 ///
/// 類型參數 /// 記錄數 public static long GetTotalCount() where T : class { try { var currentCollection = GetCollection(); return currentCollection.Count(); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } #endregion #region 數據插入 ///
/// 數據插入 ///
/// 類型參數 /// 要插入的數據對象 public static void Insert(T t) where T : class { try { var currentCollection = GetCollection(); currentCollection.Insert(t); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } #endregion #region 數據更新 ///
/// 根據查詢條件更新數據 ///
/// 類型參數 /// 待更新的對象 /// 更新的條件Lamda表達式 public static void Update(T t, System.Linq.Expressions.Expression> selector) where T : class { try { var currentCollection = GetCollection(); currentCollection.Update(t,selector); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } ///
/// 更新/插入數據(id是否存在) ///
/// 類型參數 /// 待更新的對象 public static void Update(T t) where T : class { try { var currentCollection = GetCollection(); //inserts of updates depends on whether id exists currentCollection.Save(t); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } #endregion #region 數據刪除 ///
/// 數據刪除 ///
/// 類型參數 /// 查詢的條件Lamda表達式 public static void Delete(System.Linq.Expressions.Expression> selector) where T : class { try { var currentCollection = GetCollection(); currentCollection.Remove(selector); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } #endregion } }
初次調用的時候,會讀取在站點項目下Web.Config文件中配置的數據庫服務器地址以及數據庫名稱。
?
2,Flexgrid加載的數據格式。
在官網上徘徊了很久也沒有找到Flexgrid要求的數據格式說明。還好有Firebug,但遺憾的是官方示例采用的是XML格式。如下:
1 234 … …