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了。下面是别人写的简单的使用方法:

01 var mongo = new Mongo();
02 mongo.Connect();
03 // 打开myorders数据库.
04 Database db = mongo.GetDatabase( "myorders" );
05 // 获取orders 集合.
06 IMongoCollection orders = db.GetCollection( "orders" );
07 //插入文档
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 );
13 //插入多个文档
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 );
24 //更新
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 );
32 //查找
33    // Create a specification to query the orders collection.
34    var spec = new Document();
35    spec["CustomerName"] = "Elmer Fudd";
36    // Run the query.
37    Document result = orders.FindOne( spec )
38 //linq 查找
39    // Query the orders collection.
40    IQueryable<Document> results =
41      from doc in orders.AsQueryable()
42      where doc.Key("CustomerName") == "Elmer Fudd"
43      select doc;
44    Document result = results.FirstOrDefault();
45 //删除
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() ) );
49    // Delete all docs.
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)。

原文地址:https://www.cnblogs.com/cxd4321/p/1952581.html