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

来源:清泛原创     2016-06-28 10:09:57    人气:     我有话说( 0 人参与)

清泛网(www.tsingfun.com):Mongo连接字符串形式:mongodb: 127.0.0.1:27017 插入或更新一条记录BsonDocument doc = new BsonDocument();...MongoServer serv...

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  操作 

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)