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' }
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • I guess copyDB command requires admin privilege. Check this JIRA : https://jira.mongodb.org/browse/SERVER-2846 – Abhishek Kumar May 16 '13 at 00: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) – imslavko May 16 '13 at 01:40
  • @AbhishekKumar, I actually have admin access to databases, any guesses? I feel like my syntax is wrong – imslavko May 16 '13 at 01: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. http://pastebin.com/uv41adxt – Abhishek Kumar May 16 '13 at 02: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 – imslavko May 16 '13 at 17:07
  • Any luck on this? I am stuck with this error: 'Must specify set name for replica set ConnectionStrings' – ThatBrianDude Nov 08 '17 at 14:43

2 Answers2

3

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

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • 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... – imslavko May 16 '13 at 16:55
  • 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! – imslavko May 16 '13 at 18:42
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.

iMoses
  • 4,338
  • 1
  • 24
  • 39
Brij Raj Singh - MSFT
  • 4,903
  • 7
  • 36
  • 55
  • 2
    I'm downvoting because "use admin" and db.runCommand isn't using the node driver as the question specifies. – Wyck Aug 01 '18 at 01:29