如何通过NodeJS的MongoDB本机驱动程序执行db.copyDatabase? [英] How can I execute db.copyDatabase through NodeJS's MongoDB native driver?

查看:233
本文介绍了如何通过NodeJS的MongoDB本机驱动程序执行db.copyDatabase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一个调用

mongo --eval "db.copyDatabase('somedatabase', 'somedatabase_duplicate', 'sourcehost')"

复制数据库.

目前,我坚持在Node.JS应用程序中执行相同操作.呼叫

Currently I am stuck with doing the same from within a Node.JS application. Calling

mongoCommand = `db.copyDatabase("somedatabase", "somedatabase_duplicate", "localhost")`;
db.command(mongoCommand, function(commandErr, data) {
      if(!commandErr) {
        log.info(data);
      } else {
        log.error(commandErr.errmsg);
      }
    });

总是在没有这样的命令"错误消息中出现.

Always resulsts in a "no such command" error message.

为澄清起见进行使用db.admin().command()会导致相同的问题,并使用在此处输入链接说明中建议的命令.

Edit for clarification: Using db.admin().command() results in the same problem and using the command suggested in enter link description here, too.

调用此命令或从Node.JS克隆数据库的正确方法是什么?

What's the correct way to call this command or, alternatively, to clone a database from Node.JS?

推荐答案

好,您正在尝试复制属于管理操作的数据库,因此必须与管理员帐户有关.同样,要复制数据库命令是copydb.

Well, you are trying to copy database which is administration operation so have to do with admin account. Again, to copy database command is copydb.

尝试在外壳程序db.copyDatabase中运行此命令,您将看到命令源.

try running this command in shell, db.copyDatabase and you'll see source of command.

尝试:

var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;


var url = 'mongodb://localhost:27017/test';

MongoClient.connect(url, function(err, db) {
    if (err) {
        console.log(err);
    }
    else {

        var mongoCommand = { copydb: 1, fromhost: "localhost", fromdb: "test", todb: "test_dup" };
        var admin = db.admin();

        admin.command(mongoCommand, function(commandErr, data) {
            if (!commandErr) {
                console.log(data);
            } else {
                console.log(commandErr.errmsg);
            }
            db.close();
        });
    }
});

这篇关于如何通过NodeJS的MongoDB本机驱动程序执行db.copyDatabase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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