如何通过NodeJS的MongoDB本机驱动程序执行db.copyDatabase?

时间:2016-04-04 13:15:33

标签: node.js mongodb

我有一个调用

的shell脚本
mongo --eval "db.copyDatabase('somedatabase', 'somedatabase_duplicate', 'sourcehost')"

复制数据库。

目前,我仍然坚持在Node.JS应用程序中执行相同的操作。致电

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

总是会导致“没有这样的命令”#34;错误信息。

编辑以澄清:使用db.admin()。command()会导致同样的问题并使用enter link description here中建议的命令。

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

2 个答案:

答案 0 :(得分:3)

好吧,您正在尝试复制数据库,这是管理操作,因此与管理员帐户有关。同样,复制数据库命令是copydb

尝试在shell db.copyDatabase中运行此命令,您将看到命令源。

尝试:

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();
        });
    }
});

答案 1 :(得分:0)

//core modules
const assert = require('assert')
const MongoClient = require('mongodb').MongoClient;
const moment = require('moment');
const mongo = require('mongodb')

//custom modules
let { ip, port, database } = require('./dbUpgradeConfig')
const url = `mongodb://${ip}:${port}`
let todayDate = moment().format('DD/MM/YYYY HH:mm')
console.log(todayDate)
const myDate = new Date()
console.log(myDate)
var d = Date(Date.now());
// Converting the number of millisecond in date string 
a = d.toString()

// Options for mongoDB
const mongoOptions = { useNewUrlParser: true }
let db


//TODO: handle reconnect
let connect = () => {
    return new Promise((resolve, reject) => {
        if (db) resolve()
        else {
            mongo.connect(url, mongoOptions, (err, client) => {
                if (err) reject(err)
                else {
                    db = client.db(database)
                    resolve()
                }
            })
        }
    })
}


/**
 * @description create duplicate database from current database in mongodb 
 */
let CloneDb = () => {
    return new Promise((resolve, reject) => {
        connect()
            .then(() => {
                console.log(db)
                let mongoCommand = { copydb: 1, fromhost: "localhost", fromdb: "db_name", todb: "db_name_duplicate" }
                let adminDB = db.admin()

                adminDB.command(mongoCommand, function (commandErr, data) {
                    if (!commandErr) {
                        console.log(data)
                    } else {
                        console.log(commandErr.errmsg)
                    }

                });
            })
    })
}

CloneDb().then(data => {
    // debugger;
    console.log("The clone db", data)
})