使用节点驱动程序在主机之间在Mongodb中克隆数据库 [英] Clone database in Mongodb between hosts using node driver

查看:52
本文介绍了使用节点驱动程序在主机之间在Mongodb中克隆数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

克隆 mongodb )?

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

这就是我尝试过的:

我发现节点本机mongodb驱动程序中可用的db.command.阅读文档时,我尝试了这段代码(db是名为"newdb"的目标数据库)

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' }

推荐答案

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

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

这篇关于使用节点驱动程序在主机之间在Mongodb中克隆数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆