Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent to db.repairDatabase()

Tags:

c#

mongodb

Is there a way to call MongoDB's db.repairDatabase() function from the C# driver?

I am able to compact collections:

database.RunCommand(new CommandDocument("compact","collectionname"));

But I don't manage to call repairDatabase.

like image 821
edeboursetty Avatar asked Jan 01 '26 04:01

edeboursetty


2 Answers

In response to your comment that you got an exception with the message "bad option", it turns out that the server is picky about whether you use 1 or true as the value for the repairDatabase field. The following two are equivalent and both fail because the server doesn't like "true" instead of "1":

database.RunCommand("repairDatabase");
database.RunCommnad(new CommandDocument("repairDatabase", true));

but this works:

database.RunCommnad(new CommandDocument("repairDatabase", 1));

You could report a JIRA against the server if this concerns you:

https://jira.mongodb.org/browse/SERVER

like image 188
Robert Stam Avatar answered Jan 03 '26 19:01

Robert Stam


The database.RunCommand method is overloaded. It can also take a string that is the name of command, as follows:

database.RunCommand("repairDatabase")

It also returns a CommandResult object.

like image 35
Brian Knight Avatar answered Jan 03 '26 18:01

Brian Knight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!