mongo shell中有clone和copydb命令,如何在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' }
答案 0 :(得分:4)
您是否尝试过使用db.admin().command
?
答案 1 :(得分:0)
use admin;
db.runCommand({
copydb: 1,
fromhost: "myhost",
username:"azureuser",
fromdb: "test",
todb: "test"
})
所有要求的mongo都是在运行这样的命令之前切换到“admin”db,然后运行正常。