生活随笔
收集整理的這篇文章主要介紹了
【转载】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了。下面是別人寫的簡單的使用方法:
view source print?
01 var mongo = new Mongo();
04 Database db = mongo.GetDatabase( "myorders" );
06 IMongoCollection orders = db.GetCollection( "orders" );
08 ???var order = new Document();
09 ???order["OrderAmount" ] = 57.22;
10 ???order["CustomerName" ] = "Elmer Fudd" ;
11 ???// Add the new order to the mongo orders colleciton.
12 ???orders.Insert( order );
14 ???// Create new orders.
15 ???var order1 = new Document();
16 ???order1["OrderAmount" ] = 100.23;
17 ???order1["CustomerName" ] = "Bugs Bunny" ;
18 ???var order2 = new Document();
19 ???order2["OrderAmount" ] = 0.01;
20 ???order2["CustomerName" ] = "Daffy Duck" ;
21 ???IEnumerable< Document > orderList = new List< Document > {order1, order2};
22 ???// Insert an IEnumerable.
23 ???orders.Insert( orderList );
25 ???var selector = new Document {{"CustomerName" , "Daffy Duck" }};
26 ???Document docToUpdate = orders.FindOne( selector );
27 ???Console.WriteLine( "Before Update: " + docToUpdate );
28 ???// I'm in the money!
29 ???docToUpdate["OrderAmount" ] = 1000000.00;
30 ???// Update Daffy's account before Hasaan finds him.
31 ???orders.Update( docToUpdate );
33 ???// Create a specification to query the orders collection.
34 ???var spec = new Document();
35 ???spec["CustomerName" ] = "Elmer Fudd" ;
37 ???Document result = orders.FindOne( spec )
39 ???// Query the orders collection.
40 ???IQueryable<Document> results =
41 ?????from doc in orders.AsQueryable()
42 ?????where doc.Key("CustomerName" ) == "Elmer Fudd"
44 ???Document result = results.FirstOrDefault();
46 ???// Delete documents matching a criteria.
47 ???orders.Delete( new Document {{"CustomerName" , "Elmer Fudd" }} );
48 ???Console.WriteLine( string .Format( "Document Count After Deleting Elmer Fudd: [ {0} ]" , orders.Count() ) );
50 ???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)。
總結
以上是生活随笔 為你收集整理的【转载】MongoDB基本操作 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。