C # MongoDB driver copydb admin login
I am trying to use a command copydb
in mongodb.
When I do this, I get the following exception:
Command 'copydb' failed: access denied; use admin db (response: { "errmsg" : "access denied; use
admin db", "ok" : 0.0 })
I tried to log in as administrator but failed because I am not using username and password.
How can I log in as an administrator without a username and password? Why do I need to register as an administrator if I already have the privileges to do drop
?
Thank you in advance
m_mongoDatabase.RunCommand(new CommandDocument(new BsonElement("copydb", (BsonValue) 1),
new BsonElement("fromdb", (BsonValue) from),
new BsonElement("fromhost", (BsonValue) fromHost),
new BsonElement("todb", (BsonValue) to)));
source to share
I think the problem is that you are not connecting first to the "admin" database on the target instance.
I can perform the required operation using the following code:
var client = new MongoClient(MongoUrl.Create("mongodb://localhost:27018"));
var m_mongoDatabase = client.GetServer().GetDatabase("admin");
var result = m_mongoDatabase.RunCommand(
new CommandDocument(new BsonElement("copydb", 1),
new BsonElement("fromhost", "localhost"),
new BsonElement("fromdb", "sourcedb"),
new BsonElement("todb", "targetdb")));
Note that I first get a link to the admin database. When I didn't do this and used another regular database, I got the same error as you.
So, to do this, use the admin database for the value "m_mongoDatabase"
Hope it helps
Edit: This is the same behavior when using mongodb shell, so this is not a problem with csharpdriver
source to share