MongoDB的C#驱动程序RunCommandAsync方法

4
我想从C#驱动程序运行db.printReplicationInfo()
据我所知,我只能使用MongoDatabase.RunCommandAsync来运行此处列出的命令here。为此,我有以下代码:
var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
    {"replSetGetStatus", 1}
});
var result = BsonSerializer.Deserialize<ReplicaSetStatus>(
             client.GetDatabase(ADMIN_DATABASE).RunCommandAsync(command).Result);

这让我能够运行命令并将响应反序列化为我们创建的ReplicaSetStatus对象。(client是MongoClient的一个实例)。
我的问题是,我能否使用RunCommandAsync执行像db.printReplicationInfo()这样的操作?这个命令会是什么样子?
如果不行,从C#运行这样的操作的最佳方法是什么?
看起来有人有一个类似的问题,但没有得到任何有用的答案。
谢谢!

1
注意:在Mongo shell中,db.printReplicationInfo()不会返回JSON格式的数据。如果需要手动检查,请使用db.printReplicationInfo();如果需要在脚本中使用,请使用db.getReplicationInfo()。详见https://docs.mongodb.org/v3.0/reference/method/db.printReplicationInfo/。 - user2527768
最终我放弃了C#驱动程序,改用JavaScript实现。 - mrranstrom
1个回答

0

db.printReplicationInfo() 和 db.GetReplicationInfo() 是MongoDb 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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接