问题
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