清泛IT社区
标题:
C# 操作MongoDb插入、更新、查询
[打印本页]
作者:
清泛网
时间:
2015-10-22 08:44
标题:
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();
欢迎光临 清泛IT社区 (https://bbs.tsingfun.com/)
Powered by Discuz! X3.3