3

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);

1 Answer 1

0

You need to check what is the correct command in mongodb. like sometime name need Document object instead of just string.

I am using something like this. check if this help

var name = new BsonDocument { { "name", "regions" } };
var command = new BsonDocument { { "listCollections", 1 }, { "filter", name } };
var result = Database.RunCommand<BsonDocument>(command);
var k = result.ToJson();
            

Here name is again object which I found from this documentation https://docs.mongodb.com/manual/reference/command/listCollections/

Some more help you can take from here https://zetcode.com/csharp/mongodb/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.