MongoDB db.runCommand() from C#

房东的猫 提交于 2019-12-23 17:00:32

问题


Hi I'm using C# with MongoDB Official driver v2.2.4 and I want to run db.runCommand() on the admin database.

So far i have this and i am able to connect to the admin database but db.runCommand is giving me this error "An unhandled exception of type 'System.FormatException' occurred in MongoDB.Bson.dll Additional information: JSON reader was expecting a value but found 'db'."

MongoClient client = new MongoClient();
database = client.GetDatabase("admin");
var collection = database.GetCollection<BsonDocument>("test");
var commandResult = database.RunCommand<string>(@"db.createCollection(test1)");

After I resolve this test I want to run this command from C# but I am stuck.

db.runCommand( { addshard : “localhost:10001”, name : “shard10001” } );

Any one can resolve this problem and provide me with a good explanation and example. After some search I have tried this code does seems to make more sense but still getting an error. "Additional information: Command addshard failed: no such command: 'addshard', bad cmd: '{ addshard: "192.168.1.4:27017", name: "shard1" }'."

Any ideas please of what I'm doing wrong! Thanks.

    var addShardCommand = new BsonDocument {
        { "addshard", "192.168.1.4:27017"},
        { "name", "shard1" }
    };
    var addShardResult = database.RunCommand<BsonDocument>(addShardCommand);

来源:https://stackoverflow.com/questions/38645151/mongodb-db-runcommand-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!