Search code examples
mongodb

Mongo: db.cloneCollection doesn't work in 2.4.6?


I'm trying to clone a collection from one Mongo host to another:

I have two mongo instances v.2.4.6 installed on two hosts: host1 and host2 (host1 and host2 are actually IPs but replaced with name to simplify the question). Both hosts are in the same network and I can successfully open other server's mongo console from the first server using:

on host1:

mongo --host host2

But when I do in the mongo shell of the first server:

on host1:

db.cloneCollection("host2:27017", "my_db.my_collection")

The output is:

{ "ok" : 1 }

But the my_db database is not created on the host1. I tried to create empty database but the collection isn't copied either. I tried to create empty collection in it, but the collection items are not copied.

Note: The my_db.my_collection on host2 is not empty. Have double checked that.

Guys, could you please check if the db.cloneCollection() works for you on mongo 2.4.6? If so - maybe I'm missing some prerequisite?


Solution

  • Mongodb for Windows, v 2.4.6

    First mongod with default params: port -> 27017, dbpath -> c:\data\db
    Second mongod with params: port -> 27018, dbpath -> c:\data\db1

    Logged to the second mongod. Use test db. Execute:

    db.cloneCollection("localhost:27017","db.test.so000")
    

    Not cloned. Then execute:

    db.cloneCollection("localhost:27017","so000")
    

    It was cloned. So, suggestion in this case -- do not use full path to collection.

    Updated

    Just investigated some documentation: http://docs.mongodb.org/manual/reference/command/cloneCollection/#dbcmd.cloneCollection
    If you want to use db prefix, you should run clone this way:

    db.runCommand({cloneCollection: "test.so000", from: "localhost:27017"})
    

    It will clone collection so000 as well. It will create db even if db test doesn't exist on this second server.