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

三,技術(shù)細(xì)節(jié):
1,MongoDB數(shù)據(jù)庫(kù)操作。
ADO.NET雖然很強(qiáng)大,但我們通常需要自己動(dòng)手寫(xiě)一個(gè)SqlHelper。MongoDB也一樣,我們?nèi)杂斜匾隍?qū)動(dòng)的基礎(chǔ)上對(duì)常用的數(shù)據(jù)操作進(jìn)行封裝。下面貼出本鳥(niǎo)寫(xiě)的MongoDB數(shù)據(jù)庫(kù)操作類(lèi)。大多數(shù)方法都與數(shù)據(jù)查詢(xún)相關(guān)。貼出主要代碼:
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;
///
/// 當(dāng)前Mongo引用
///
private static Mongo mongo;
///
/// 初始化數(shù)據(jù)庫(kù)配置
///
static MongoDBHelper()
{
ConnectionString = ConfigurationManager.AppSettings["connString"];
DatabaseName = ConfigurationManager.AppSettings["currentDB"];
}
///
/// 當(dāng)前Mongo對(duì)象
///
private static Mongo CurrentMongo
{
get
{
return new Mongo(ConnectionString);
}
}
///
/// 當(dāng)前操作集合
///
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;
}
}
///
/// 獲取當(dāng)前Mongo數(shù)據(jù)庫(kù)對(duì)象
///
/// 當(dāng)前Mongo數(shù)據(jù)庫(kù)對(duì)象
private static IMongoDatabase GetCurrentDataBase()
{
return mongo.GetDatabase(DatabaseName);
}
#endregion
#region 數(shù)據(jù)查詢(xún)
///
/// 獲取排序規(guī)則
///
private static IndexOrder GetIndexOrder(DataOrder order)
{
IndexOrder @orderby = order == DataOrder.Ascending ?
IndexOrder.Ascending : IndexOrder.Descending;
return orderby;
}
///
/// 根據(jù)條件進(jìn)行查詢(xún),并進(jìn)行分頁(yè)
///
/// 類(lèi)型參數(shù)
/// 條件Lamda表達(dá)式
/// 當(dāng)前頁(yè)索引
/// 分頁(yè)大小
/// 結(jié)果集
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();
}
}
///
/// 根據(jù)條件進(jìn)行查詢(xún),并進(jìn)行分頁(yè)和排序
///
/// 類(lèi)型參數(shù)
/// 條件Lamda表達(dá)式
/// 當(dāng)前頁(yè)索引
/// 分頁(yè)大小
/// 排序規(guī)則
/// 排序字段
/// 結(jié)果集
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();
}
}
///
/// 根據(jù)條件查詢(xún)一個(gè)對(duì)象
///
/// 類(lèi)型參數(shù)
/// 查詢(xún)條件Lamda表達(dá)式
/// 查詢(xún)對(duì)象
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();
}
}
///
/// 根據(jù)查詢(xún)條件獲取總記錄數(shù)
///
/// 類(lèi)型參數(shù)
/// 查詢(xún)條件
/// 記錄數(shù)
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();
}
}
///
/// 獲取總記錄數(shù)
///
/// 類(lèi)型參數(shù)
/// 記錄數(shù)
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 數(shù)據(jù)插入
///
/// 數(shù)據(jù)插入
///
/// 類(lèi)型參數(shù)
/// 要插入的數(shù)據(jù)對(duì)象
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 數(shù)據(jù)更新
///
/// 根據(jù)查詢(xún)條件更新數(shù)據(jù)
///
/// 類(lèi)型參數(shù)
/// 待更新的對(duì)象
/// 更新的條件Lamda表達(dá)式
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();
}
}
///
/// 更新/插入數(shù)據(jù)(id是否存在)
///
/// 類(lèi)型參數(shù)
/// 待更新的對(duì)象
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 數(shù)據(jù)刪除
///
/// 數(shù)據(jù)刪除
///
/// 類(lèi)型參數(shù)
/// 查詢(xún)的條件Lamda表達(dá)式
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
}
}
初次調(diào)用的時(shí)候,會(huì)讀取在站點(diǎn)項(xiàng)目下Web.Config文件中配置的數(shù)據(jù)庫(kù)服務(wù)器地址以及數(shù)據(jù)庫(kù)名稱(chēng)。
2,F(xiàn)lexgrid加載的數(shù)據(jù)格式。
在官網(wǎng)上徘徊了很久也沒(méi)有找到Flexgrid要求的數(shù)據(jù)格式說(shuō)明。還好有Firebug,但遺憾的是官方示例采用的是XML格式。如下:
1 234
...
...
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com