MongoDBとc#(二)簡単な例1.7バージョンで駆動

22015 ワード

          //       
         1.7 MongoServer
            //MongoServer server =  MongoDB.Driver.MongoServer.Create(strconn);


     // , , ip+ const string connectionString = "mongodb://city:[email protected]: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 }