清泛IT社区

标题: C# 操作MongoDb插入、更新、查询 [打印本页]

作者: 清泛网    时间: 2015-10-22 08:44
标题: C# 操作MongoDb插入、更新、查询
Mongo连接字符串形式:mongodb://127.0.0.1:27017
  1. // 插入或更新一条记录
  2. BsonDocument doc = new BsonDocument();
  3. ...
  4. MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
  5. MongoDatabase db = server.GetDatabase("admin");
  6. db.GetCollection("MonLogs").Save<BsonDocument>(doc);
复制代码
  1. // 查询记录
  2. MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
  3. MongoDatabase db = server.GetDatabase("admin");
  4. var collection = db.GetCollection("MonLogs").Find(query);
  5. if (collection != null)
  6. {
  7.     foreach (var col in collection)
  8.     {
  9.         string id = MongoDataHelper.GetString(col, "_id");
  10.     }
  11. }
复制代码
提示函数过期警告:
warning CS0618: “MongoDB.Driver.MongoServer.Create(string)”已过时:“Use MongoClient.GetServer instead.”

解决:
MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
改为:
MongoServer server = new MongoClient(Config.MonLogMongoDbStr).GetServer();










欢迎光临 清泛IT社区 (https://bbs.tsingfun.com/) Powered by Discuz! X3.3