MongoDB的第一次親密接觸
園子里已經有不少朋友發過MongoDB的帖子,但是都比較高端,我在這里就寫下比較基礎的應用,算是MongoDB的第一次接觸有所了解。呵呵。我們去Mongodb.org看一看。首頁赫然寫著 The Best Features of Document Databases,Key-Value Stores,and RDBMSes。意思是最牛逼的文檔數據庫,鍵值對的存儲并且是RDBMS(relational database management system關系型數據庫管理系統)。下面解釋說MongoDB縮小了KV存儲和傳統RDBMS的差距。
Document-oriented storage
Json格式的文檔存儲。用過ajax的朋友都知道Json長啥樣{"key","value"}
Full Index Support
和數據庫一樣,MongoDB也支持索引。
Replication & High Availability
MongoDB也良好的支持多臺Server之間的數據同步,保證一個掛掉還能繼續干活。
Auto-Sharding
自動發現Server,負載均衡,避免單點故障。
Querying
豐富的基于document的查詢。后面我們會舉例介紹。
Fast In-Place Updates
會根據不同的情況進行數據更新
Map/Reduce
靈活的聚集和數據處理。
GridFS
GirdFS是MongoDB的大文件存儲系統,比如圖片、音頻、視頻。
呵呵,心動不如行動,我們可以試試他的Try It Out,進行命令行的操作。當然,這不是C#。
下載MongoDB,自己使用版本無所謂,服務器使用如果處理大文件,就要用64bit的,因為32的只能處理<2G的文件。 關于安裝,很多朋友的博文都有介紹,搜索一下就可以了,都是圖文并茂的。但是有一點我要提醒下,就是關于安裝成Windows 服務,是有點問題的,起碼Windows 7是這樣,我們首先要建立一個log.txt,然后使用--logpath ="\"d:\mongodb\log.txt""--install來進行安裝,然后去注冊表把此服務的值改成--dbpath="\"d:\mongodb\db\""--service。因為很多人的介紹不是用--install,這樣我是安裝不成功的。
C#客戶端
我們.NET自然要去使用C#來和MongoD服務進行通信,幸好有社區的好心人寫了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兩種方式。如果擔心Linq的性能問題可以使用document。 引用Mongodb-csharp的dll,我們就可以操作MongoDB了。下面是別人寫的簡單的使用方法:
var mongo = new Mongo(); mongo.Connect(); // 打開myorders數據庫. 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() );
如果向像SqlServer那樣查看數據庫的數據,目前也有很多客戶端支持,MongoVUE不錯,我用過。我想大家可以試著自己寫哥客戶端:)
當我們查看具體集合的時候發現一個問題,就是MongoDB會自動增加一個_id字段,其值長的很像Guid,默認為索引字段。如果我們要自定義這個字段的話,在設計實體類時,在“主鍵”字段上增加一個屬性[MongoId]即可。
在設計實體類時,字段也不能用于偏僻的類型,比如XElement,在讀的時候Mongodb-csharp反序列化會拋出異常,所以建議使用string來代替。如果不愛使用document,喜歡linq查詢,存儲的時候如果某個集合存儲某個類型的各種子類,在GetCollection<T>的時候也不能完成正確子類的反序列化,這些問題大家在使用的過程中會慢慢發現,也可以郵件訂閱Mongodb-csharp的google group(發送空郵件到mongodb-csharp@googlegroups.com)。