2

There are clone and copydb commands available in mongo shell, how to reach them in mongo node native driver(mongodb)?

That's what I have tried:

I discovered the db.command available in node native mongodb driver. Reading documentation I tried this piece of code (db is the destination db named 'newdb')

db = db.db('newdb');
db.addUser('newdbuser', 'newdbpass', {}, function (err) {
    err && console.log(err);
    console.log(authUrlForDb(config.MONGO_HOSTS));
    db.command({
        copydb: 1,
        fromhost: config.MONGO_HOSTS,
        fromdb: config.MOTHER_DB, // some database name
        todb: 'newdb',
        username: config.ADMIN_USERNAME,  //
        key: {
            username: config.ADMIN_USERNAME,
            password: config.ADMIN_PASSWORD
        }
    }, function (err, res) {
        console.log(config.MONGO_HOSTS);
        console.log(err, res);
        db.close();
    });
});

Which fails and logs this:

hostname1.host.io,hostname2.host.io
null { ok: 0, errmsg: 'access denied; use admin db' }
6
  • I guess copyDB command requires admin privilege. Check this JIRA : jira.mongodb.org/browse/SERVER-2846 Commented May 16, 2013 at 0:24
  • @AbhishekKumar, thanks for this link, does it mean copying from mongohq will fail in any case? (As I don't own admin rights on cluster on MongoHQ) Commented May 16, 2013 at 1:40
  • @AbhishekKumar, I actually have admin access to databases, any guesses? I feel like my syntax is wrong Commented May 16, 2013 at 1:46
  • 'access denied; use admin db'. The error says that you have to connect to admin database giving the privileges and then try copying. I tried the same in my localhost copying my 'test' database and without authenticating my 'admin' database and it failed. I had to log in to my 'admin' database and then I have to run the command and it succeeded. pastebin.com/uv41adxt Commented May 16, 2013 at 2:07
  • @AbhishekKumar, I am not sure if it works the same way in node driver. The code you provide uses mongo shell, correct? My guess is noa gave the same solution but for node driver Commented May 16, 2013 at 17:07

2 Answers 2

3

Have you tried using db.admin().command?

Sign up to request clarification or add additional context in comments.

2 Comments

it gives me 'unauthorized' but it is already something :) My guess is: I should provide valid nonce, I will try one more time once I find this nonce property...
It should work, looks like my problem is very specific and general solution just doesn't apply, I will try dump/restore. Thanks for the answer!
0

dude, you should essentially just try

use admin;

db.runCommand({
  copydb: 1,
  fromhost: "myhost",
  username:"azureuser",
  fromdb: "test",
  todb: "test"
})

All mongo asking for is the switch to "admin" db before running such a command, and then it will run fine.

1 Comment

I'm downvoting because "use admin" and db.runCommand isn't using the node driver as the question specifies.

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.