3

I want to be able to execute a database method using the C# SDK (2.2.4). Take db.version() for example.

I've tried to play around with Database.RunCommand, but with no luck:

var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
    { "version", 1 }
});

var versionResult = Database.RunCommand(command);

Exception:

MongoDB.Driver.MongoCommandException: Command version failed: no such command: 'version', bad cmd: '{ version: 1 }'.

2 Answers 2

3

There's not always a direct mapping between the shell database methods you link to and the underlying database commands that are available via Database.RunCommand. The available commands are listed here, and to get the server version you could use the serverStatus command:

var version = db.RunCommand<dynamic>(new BsonDocument("serverStatus", 1)).version;
Sign up to request clarification or add additional context in comments.

Comments

1

I can get dbstats using above approach but not version. (in both mongo shell and .net)

But then I get same error if i try to get version this way in mongo shell

db.runCommand({version:1})
{
    "ok" : 0,
    "errmsg" : "no such command: 'version', bad cmd: '{ version: 1.0 }'",
    "code" : 59
}

There seems to be different way to get version? as runCommand supports operations which are supported on Mongo shell?

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.