|
園子里已經(jīng)有不少朋友發(fā)過MongoDB的帖子,但是都比較高端,我在這里就寫下比較基礎(chǔ)的應(yīng)用,算是MongoDB的第一次接觸有所了解。呵呵。我們?nèi)ongodb.org看一看。首頁赫然寫著 The Best Features of Document Databases,Key-Value Stores,and RDBMSes。意思是最牛逼的文檔數(shù)據(jù)庫,鍵值對的存儲并且是RDBMS(relational database management system關(guān)系型數(shù)據(jù)庫管理系統(tǒng))。下面解釋說MongoDB縮小了KV存儲和傳統(tǒng)RDBMS的差距。
Document-oriented storage
Json格式的文檔存儲。用過ajax的朋友都知道Json長啥樣{"key","value"}
Full Index Support
和數(shù)據(jù)庫一樣,MongoDB也支持索引。
Replication & High Availability
MongoDB也良好的支持多臺Server之間的數(shù)據(jù)同步,保證一個掛掉還能繼續(xù)干活。
Auto-Sharding
自動發(fā)現(xiàn)Server,負(fù)載均衡,避免單點(diǎn)故障。
Querying
豐富的基于document的查詢。后面我們會舉例介紹。
Fast In-Place Updates
會根據(jù)不同的情況進(jìn)行數(shù)據(jù)更新
Map/Reduce
靈活的聚集和數(shù)據(jù)處理。
GridFS
GirdFS是MongoDB的大文件存儲系統(tǒng),比如圖片、音頻、視頻。
呵呵,心動不如行動,我們可以試試他的Try It Out,進(jìn)行命令行的操作。當(dāng)然,這不是C#。
下載MongoDB,自己使用版本無所謂,服務(wù)器使用如果處理大文件,就要用64bit的,因為32的只能處理<2G的文件。 關(guān)于安裝,很多朋友的博文都有介紹,搜索一下就可以了,都是圖文并茂的。但是有一點(diǎn)我要提醒下,就是關(guān)于安裝成Windows 服務(wù),是有點(diǎn)問題的,起碼Windows 7是這樣,我們首先要建立一個log.txt,然后使用--logpath ="/"d:/mongodb/log.txt""--install來進(jìn)行安裝,然后去注冊表把此服務(wù)的值改成--dbpath="/"d:/mongodb/db/""--service。因為很多人的介紹不是用--install,這樣我是安裝不成功的。
C#客戶端
我們.NET自然要去使用C#來和MongoD服務(wù)進(jìn)行通信,幸好有社區(qū)的好心人寫了MongoDB的.NET Driver 。有三種,Mongodb-csharp、Simple-cshapr和NoRM(http://www.mongodb.org/display/DOCS/C+Sharp+Language+Center )。我就使用Mongodb-csharp(http://github.com/samus/mongodb-csharp),因為他支持Document和Linq兩種方式。如果擔(dān)心Linq的性能問題可以使用document。 引用Mongodb-csharp的dll,我們就可以操作MongoDB了。下面是別人寫的簡單的使用方法:
var mongo = new Mongo();mongo.Connect();// 打開myorders數(shù)據(jù)庫.Database db = mongo.GetDatabase( "myorders" );// 獲取orders 集合.IMongoCollection orders = db.GetCollection( "orders" );//插入文檔 var order = new Document(); order["OrderAmount"] = 57.22; order["CustomerName"] = "Elmer Fudd"; // Add the new order to the mongo orders colleciton. orders.Insert( order );//插入多個文檔 // Create new orders. var order1 = new Document(); order1["OrderAmount"] = 100.23; order1["CustomerName"] = "Bugs Bunny"; var order2 = new Document(); order2["OrderAmount"] = 0.01; order2["CustomerName"] = "Daffy Duck"; IEnumerable< Document > orderList = new List< Document > {order1, order2}; // Insert an IEnumerable. orders.Insert( orderList );//更新 var selector = new Document {{"CustomerName", "Daffy Duck"}}; Document docToUpdate = orders.FindOne( selector ); Console.WriteLine( "Before Update: " + docToUpdate ); // I'm in the money! docToUpdate["OrderAmount"] = 1000000.00; // Update Daffy's account before Hasaan finds him. orders.Update( docToUpdate );//查找 // Create a specification to query the orders collection. var spec = new Document(); spec["CustomerName"] = "Elmer Fudd"; // Run the query. Document result = orders.FindOne( spec )//linq 查找 // Query the orders collection. IQueryable<Document> results = from doc in orders.AsQueryable() where doc.Key("CustomerName") == "Elmer Fudd" select doc; Document result = results.FirstOrDefault();//刪除 // Delete documents matching a criteria. orders.Delete( new Document {{"CustomerName", "Elmer Fudd"}} ); Console.WriteLine( string.Format( "Document Count After Deleting Elmer Fudd: [ {0} ]", orders.Count() ) ); // Delete all docs. orders.Delete( new Document() );
it知識庫:MongoDB的第一次親密接觸,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。