0

I'm trying to clone a mongodb collection with the indexs from two different MongoDB instances. After googling around, I found that cloneCollection would be my best bet. I've tried to make my code nice and simple, but I never seem to get the actual clone command to run. 'Started' and 'Authed' pop up in my console, but the command never runs. What is it I'm doing wrong? Thanks.

mongodb = require("mongodb")

#####################
# MONGODB CONFIG
#####################
console.log "Started"
Server = mongodb.Server
Db = mongodb.Db

server = new Server("example.com", 27017, {})
db = new Db("databaseTwo", server,
    safe: true
)

db.open (err, db) ->
    db.authenticate "user", "password", (err, res) ->
        throw err if err
        console.log "Authed"

db.command
    cloneCollection: "databaseOne.helloThereCollection"
    from: "example.com:6001"
    key:
        username: 'user'
        password: 'password'
, (err, cb) ->
    console.log err if err
    console.log 'Command Ran'
    cb null
Dustin
  • 6,207
  • 19
  • 61
  • 93
  • Possible duplicate of this [question](http://stackoverflow.com/questions/16576541/clone-database-in-mongodb-between-hosts-using-node-driver). Try using [`db.admin.command()`](http://mongodb.github.io/node-mongodb-native/api-generated/admin.html#command) – Anand Jayabalan Mar 24 '14 at 20:41
  • Not a dup. He's trying to clone an entire data, myself just a collection with indexs. Gave it a shot, admin doesn't have a method "command". I would think my script above would atleast spit out an error if there was a collection/database problem. – Dustin Mar 24 '14 at 20:47
  • ah ok, my bad! it isn't a dup. – Anand Jayabalan Mar 24 '14 at 21:11
  • Maybe not a duplicate, but you **did notice** the "command" is run **inside** the "open" callback didn't you? So what do you think happens when something like this is **outside** of that callback? – Neil Lunn Mar 24 '14 at 23:14
  • Try moving the db.command inside the db.open callback. I think the process may be exiting before that command callback is run. You're not getting errors, is it cloning the collection but not running the console logs ("Command Ran")? – jcollum Mar 25 '14 at 16:23

0 Answers0