C# 操作MongoDb插入、更新、查询

清泛原创
Mongo连接字符串形式:mongodb://127.0.0.1:27017
// 插入或更新一条记录
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();

C# MongoDB 操作

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部