如何从C#驱动程序获取db.currentOp() - mongodb

时间:2014-01-30 08:16:46

标签: mongodb

我正在尝试监控mongoServer,包括运行进程及其时间。

有没有办法在C#中获取db.currentOp()?

2 个答案:

答案 0 :(得分:2)

Google可能会回答这个问题...... GetCurrentOp怎么样?

MongoClient client = new MongoClient("mongodb://localhost");
var server = client.GetServer();
var db = server.GetDatabase("test", WriteConcern.Acknowledged);
var currentOp = db.GetCurrentOp();

答案 1 :(得分:2)

使用2.0驱动程序:

MongoClient client = new MongoClient("mongodb://localhost");
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("$cmd.sys.inprog");
var currentOp = collection.Find(new BsonDocument()).FirstOrDefault();

将我们的MongoDB实例更新到3.4.10(从3.0开始)后,之前的答案对我来说不再有效。以下是我必须将其更新为:

MongoClient client = new MongoClient("mongodb://root:password@localhost?authSource=admin");
var db = client.GetDatabase("admin");
var command = new BsonDocument {
    { "currentOp", "1"},
};
var currentOp = db.RunCommand<BsonDocument>(command);