[CSHARP-1808] RunCommand against a --replset Connection Created: 24/Oct/16  Updated: 05/Apr/19  Resolved: 22/Jan/18

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.3
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Hu Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When I create a mongod with --replset option,the admin database will not create.
But some command is need to run against admin database.
In my case,I want to execute "replSetInitiate" command by my application.
if I create mongod with --replset,"admin" database is not exist.(And I also can't create a admin database by application)
if I create mongod without --replset ,replSetInitiate will raise a exception.
Would you kindly tell me a way to RunCommand without a "admin" Database.



 Comments   
Comment by Robert Stam [ 22/Jan/18 ]

You can run any command you want against the admin database like this:

var adminDatabase = client.GetDatabase("admin");
BsonDocument command = ...
var result = adminDatabase.RunCommand<BsonDocument>(command);

We won't be adding helper methods for things initiating a replica set because so few applications would use that and we don't want to expand the surface area of the API unnecessarily, and there is already a way to that (or run any command) using RunCommand.

Comment by Hu [ 25/Oct/16 ]

Not only replset but also sharding command is needed.
So I think C# driver can add some shell Command Helper at next version.
Thank you for your work.

Comment by Hu [ 25/Oct/16 ]

Thank you for your reply.
What I need is a way to eval ,or run command to a connection via C#.
(Mongo Driver has a method MongoServer.RunAdminCommand and be removed since 1.4)

C# drive has a method db.runCommand,but some method must run as admin.
1.A mongod instance with --repl option is create without a admin.
so admin.runcommand does not work.
2.When I try to create a database with the name "admin",it is not allowed because it is a replica.
3.My application is a administrator tool , so I hope this tool can help user Init replset with GUI.
so I really need a way to run "replSetInitiate" or other shell via C#.

Comment by Robert Stam [ 24/Oct/16 ]

If you're going to connect to replica set member directly before the replica set has been initiated you need to use a direct connection.

Use a connection string similar to the following:

mongodb://hostname:port/?connect=direct

Once the replica set has been initiated you normally would no longer use direct connections.

Generated at Wed Feb 07 21:40:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.