使用节点驱动程序在主机之间克隆Mongodb中的数据库

时间:2013-05-15 23:20:11

标签: node.js mongodb

mongo shell中有clonecopydb命令,如何在mongo节点本机驱动程序(mongodb)中访问它们?

这就是我的尝试:

我在节点本机mongodb驱动程序中发现了db.command。阅读文档我尝试了这段代码(db是名为'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();
    });
});

哪个失败并记录下来:

hostname1.host.io,hostname2.host.io
null { ok: 0, errmsg: 'access denied; use admin db' }

2 个答案:

答案 0 :(得分:4)

您是否尝试过使用db.admin().command

答案 1 :(得分:0)

老兄,你基本上应该尝试

use admin;

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

所有要求的mongo都是在运行这样的命令之前切换到“admin”db,然后运行正常。