作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想跑 db.printReplicationInfo()
来自 c# 驱动程序。
据我所知,我只能运行列出的命令 here使用 MongoDatabase.RunCommandAsync
.为此,我有如下代码:
var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
{"replSetGetStatus", 1}
});
var result = BsonSerializer.Deserialize<ReplicaSetStatus>(
client.GetDatabase(ADMIN_DATABASE).RunCommandAsync(command).Result);
ReplicaSetStatus
我们创建的对象。 (客户端是 MongoClient 的一个实例)。
RunCommandAsync
执行类似
db.printReplicationInfo()
的操作?命令会是什么样的?
最佳答案
db.printReplicationInfo() 和 db.GetReplicationInfo() 是 MongoDb shell 函数,不能在 shell 外使用。
您可以在 mongo shell 中自己查看函数:
rs:PRIMARY> db.getReplicationInfo
function() {
var localdb = this.getSiblingDB("local");
var result = {};
var oplog;
var localCollections = localdb.getCollectionNames();
if (localCollections.indexOf('oplog.rs') >= 0) {
oplog = 'oplog.rs';
} else {
result.errmsg = "replication not detected";
return result;
}
var ol = localdb.getCollection(oplog);
var ol_stats = ol.stats();
if (ol_stats && ol_stats.maxSize) {
result.logSizeMB = ol_stats.maxSize / (1024 * 1024);
} else {
result.errmsg = "Could not get stats for local." + oplog + " collection. " +
"collstats returned: " + tojson(ol_stats);
return result;
}
result.usedMB = ol_stats.size / (1024 * 1024);
result.usedMB = Math.ceil(result.usedMB * 100) / 100;
var firstc = ol.find().sort({$natural: 1}).limit(1);
var lastc = ol.find().sort({$natural: -1}).limit(1);
if (!firstc.hasNext() || !lastc.hasNext()) {
result.errmsg =
"objects not found in local.oplog.$main -- is this a new and empty db instance?";
result.oplogMainRowCount = ol.count();
return result;
}
var first = firstc.next();
var last = lastc.next();
var tfirst = first.ts;
var tlast = last.ts;
if (tfirst && tlast) {
tfirst = DB.tsToSeconds(tfirst);
tlast = DB.tsToSeconds(tlast);
result.timeDiff = tlast - tfirst;
result.timeDiffHours = Math.round(result.timeDiff / 36) / 100;
result.tFirst = (new Date(tfirst * 1000)).toString();
result.tLast = (new Date(tlast * 1000)).toString();
result.now = Date();
} else {
result.errmsg = "ts element not found in oplog objects";
}
return result;
}
rs:PRIMARY> db.printReplicationInfo
function() {
var result = this.getReplicationInfo();
if (result.errmsg) {
var isMaster = this.isMaster();
if (isMaster.arbiterOnly) {
print("cannot provide replication status from an arbiter.");
return;
} else if (!isMaster.ismaster) {
print("this is a slave, printing slave replication info.");
this.printSlaveReplicationInfo();
return;
}
print(tojson(result));
return;
}
print("configured oplog size: " + result.logSizeMB + "MB");
print("log length start to end: " + result.timeDiff + "secs (" + result.timeDiffHours + "hrs)");
print("oplog first event time: " + result.tFirst);
print("oplog last event time: " + result.tLast);
print("now: " + result.now);
}
关于c# - MongoDB C# 驱动程序 RunCommandAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440956/
我想跑 db.printReplicationInfo()来自 c# 驱动程序。 据我所知,我只能运行列出的命令 here使用 MongoDatabase.RunCommandAsync .为此,我有
我是一名优秀的程序员,十分优秀!