简体   繁体   中英

Run MongoDB commands from C#

I want to be able to run any MongoDB command from C#. I know that this can be done. I start with a simple example, instead of using the dropDatabase method from the C# driver I am trying to drop a database using the db.runCommand method as follows.

I have tried in two ways, passing the command as a string and also passing the command as a BsonDocument but nothing is working and I don't have any clues where I'm wrong even after researching on the internet I cannot find a suitable example.

I'm having a really hard time to identify why this piece of code is not working.

Command passed as a string :

database.RunCommand<string>("{dropdatabase : 1}");

Command passed as a BSON document :

var command = new BsonDocument { {"dropdatabase", "1" } };
var execute = database.RunCommand<BsonDocument>(command);

You can use a JsonCommand like this:

var command = new JsonCommand<BsonDocument>("{ dropDatabase: 1 }");
db.RunCommand(command);

or use a CommandDocument like this:

var command = new CommandDocument("dropDatabase", 1);
db.RunCommand<BsonDocument>(command);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM