C# 操作MongoDb插入、更新、查询
清泛原创
Mongo连接字符串形式:mongodb://127.0.0.1:27017
warning CS0618: “MongoDB.Driver.MongoServer.Create(string)”已过时:“Use MongoClient.GetServer instead.”
解决:
MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
改为:
MongoServer server = new MongoClient(Config.MonLogMongoDbStr).GetServer();
// 插入或更新一条记录
BsonDocument doc = new BsonDocument();
...
MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
MongoDatabase db = server.GetDatabase("admin");
db.GetCollection("MonLogs").Save<BsonDocument>(doc);
// 查询记录
MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
MongoDatabase db = server.GetDatabase("admin");
var collection = db.GetCollection("MonLogs").Find(query);
if (collection != null)
{
foreach (var col in collection)
{
string id = MongoDataHelper.GetString(col, "_id");
}
}
提示函数过期警告:warning CS0618: “MongoDB.Driver.MongoServer.Create(string)”已过时:“Use MongoClient.GetServer instead.”
解决:
MongoServer server = MongoServer.Create(Config.MonLogMongoDbStr);
改为:
MongoServer server = new MongoClient(Config.MonLogMongoDbStr).GetServer();