MongoDB与c#(二)简单例子 使用1.7版本驱动

          //创建数据库链接
        在1.7的版本驱动中这样写是会报 MongoServer方法已过时的
            //MongoServer server =  MongoDB.Driver.MongoServer.Create(strconn);


     //带有用户名,密码的如下写法,不带的则直接ip+端口就可以 const string connectionString = "mongodb://city:liyang@192.168.1.211:27017"; //得到一个客户端对象的引用 GetServer()对服务器对象的引用 var Server = new MongoClient(connectionString).GetServer(); //到一个数据库对象的引用 var client = Server.GetDatabase("City"); //对一组对象的引用 var collection = client.GetCollection<citys>("citys"); //插入一个 实体 for (int i = 0; i < dt.Rows.Count; i++) { collection.Insert(new citys { province = dt.Rows[i][0].ToString(), city = dt.Rows[i][1].ToString(), county = dt.Rows[i][2].ToString(), areacode = "0" + dt.Rows[i][3].ToString(), postalcode = dt.Rows[i][3].ToString() }); }


以下是git上的帮助文档 地址是:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/
  1 将一个引用添加到c#司机dll
  2 
  3 右键单击 引用 文件夹在Visual Studio的解决方案 探险家和选择 添加 参考…… 。 导航到文件夹 c#驱动程序dll被安装并添加一个引用以下 dll:
  4 
  5     MongoDB.Bson.dll
  6     MongoDB.Driver.dll
  7 
  8 作为一种替代方法可以使用 NuGet 包管理器添加c# 驱动程序包来您的解决方案。
  9 添加所需的 使用 语句
 10 
 11 作为一个最低需要使用语句如下:
 12 
 13 using MongoDB.Bson;
 14 using MongoDB.Driver;
 15 
 16 此外,您会经常添加一个或多个使用 声明:
 17 
 18 using MongoDB.Driver.Builders;
 19 using MongoDB.Driver.GridFS;
 20 using MongoDB.Driver.Linq;
 21 
 22 还有其他名称空间,只会在特殊的需要 用例。
 23 得到一个客户端对象的引用
 24 
 25 最简单的方法获得一个客户对象的引用是使用 连接字符串:
 26 
 27 var connectionString = "mongodb://localhost";
 28 var client = new MongoClient(connectionString);
 29 
 30 如果你想将客户端对象存储在一个全局变量。 MongoClient 是线程安全的。
 31 对服务器对象的引用
 32 
 33 要从客户端到服务器对象的引用对象,写 这样的:
 34 
 35 var server = client.GetServer();
 36 
 37 到一个数据库对象的引用
 38 
 39 去到一个数据库对象的引用从服务器对象,写 这样的:
 40 
 41 var database = server.GetDatabase("test"); // "test" is the name of the database
 42 
 43 如果您使用多个数据库,调用 GetDatabase 再次为每个 您想要使用数据库。
 44 BsonDocument 对象模型与您自己的域类
 45 
 46 有两种方法可以处理集合:
 47 
 48     使用 BsonDocument 对象模型
 49     使用自己的域类
 50 
 51 您将使用 BsonDocument 当数据对象模型 工作是如此的*形式,它将是困难的或不可能的 定义的域类。
 52 
 53 因为它是如此容易使用自己的域类 快速启动将假设你要这样做。 c#驱动程序 提供,他们可以处理您的域类:
 54 
 55     有一个无参数的构造函数
 56     定义公共读/写数据的字段或属性 存储在数据库中
 57 
 58 这些需求在本质上是相同的。net的实施 XmlSerializer。
 59 
 60 此外,如果您的域类将被用作根 文档必须包含一个 ID 字段或属性(通常是命名 ID 尽管你可以覆盖,如果必要)。 通常情况下, ID 将类型的 ObjectId ,但没有限制的类型 的成员。
 61 
 62 考虑下面的类定义:
 63 
 64 public class Entity
 65 {
 66     public ObjectId Id { get; set; }
 67 
 68     public string Name { get; set; }
 69 }
 70 
 71 对一组对象的引用
 72 
 73 你会得到一个包含引用集合 实体 文件是这样的:
 74 
 75 // "entities" is the name of the collection
 76 var collection = database.GetCollection<Entity>("entities");
 77 
 78 插入文档
 79 
 80 插入一个 实体 :
 81 
 82 var entity = new Entity { Name = "Tom" };
 83 collection.Insert(entity);
 84 var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
 85 
 86 找到一个现有的文档
 87 
 88 在这个示例中,我们将读回 实体 假设我们知道 ID 值:
 89 
 90 var query = Query<Entity>.EQ(e => e.Id, id);
 91 var entity = collection.FindOne(query);
 92 
 93 查询<单位> .EQ 使用 查询< T > 构建器类来构建 查询。 lambda表达式 E = > e.Id 是翻译 _ID 。 这是 字段的名称存储在数据库中。
 94 
 95 请注意
 96 
 97 通常的名称字段在数据库中是完全相同的 正如它的名字域类中的字段或属性,但是 ID 是一个例外,映射到吗 _ID 在数据库中。
 98 
 99 其他查询操作符包括: GT , 一种 , 在 , LT , LTE , 附近 , 东北 , 和 , 或 (和其他一些更多 专业的)。
100 保存一个文档
101 
102 你可以保存更改现有的文档如下:
103 
104 entity.Name = "Dick";
105 collection.Save(entity);
106 
107 更新现有的文档
108 
109 另一种选择 保存 是 更新 。 所不同的是, 保存 将整个文档发送回服务器,但是 更新 发的变化。 例如:
110 
111 var query = Query<Entity>.EQ(e => e.Id, id);
112 var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifiers
113 collection.Update(query, update);
114 
115 下面的例子使用了 更新< T > 构建器轻松地构建更新 修饰符。
116 删除一个现有的文档
117 
118 删除一个现有的文档集合你写:
119 
120 var query = Query<Entity>.EQ(e => e.Id, id);
121 collection.Remove(query);
122 
123 你不需要调用连接或断开连接
124 
125 c#司机有一个连接池使用连接到服务器 效率。 不需要电话 连接 或 断开 ; 让司机照顾连接(调用 连接 是无害的,但是打电话呢 断开 是不好的,因为它关闭 连接池中的连接)。
126 完整的示例程序
127 
128 using System;
129 using System.Collections.Generic;
130 using System.Linq;
131 using System.Text;
132 
133 using MongoDB.Bson;
134 using MongoDB.Driver;
135 using MongoDB.Driver.Builders;
136 
137 namespace ConsoleApplication1
138 {
139     public class Entity
140     {
141         public ObjectId Id { get; set; }
142         public string Name { get; set; }
143     }
144 
145     class Program
146     {
147         static void Main(string[] args)
148         {
149             var connectionString = "mongodb://localhost";
150             var client = new MongoClient(connectionString);
151             var server = client.GetServer();
152             var database = server.GetDatabase("test");
153             var collection = database.GetCollection<Entity>("entities");
154 
155             var entity = new Entity { Name = "Tom" };
156             collection.Insert(entity);
157             var id = entity.Id;
158 
159             var query = Query<Entity>.EQ(e => e.Id, id);
160             entity = collection.FindOne(query);
161 
162             entity.Name = "Dick";
163             collection.Save(entity);
164 
165             var update = Update<Entity>.Set(e => e.Name, "Harry");
166             collection.Update(query, update);
167 
168             collection.Remove(query);
169         }
170     }
171 }