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了。下面是别人写的简单的使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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)。