I am trying to call the mongodb cloneCollection command from ruby. Based on the first question at https://github.com/mongodb/mongo-ruby-driver/wiki/FAQ I made this test script:
require "mongo"
include Mongo
db = MongoClient.new("localhost", 27017).db("test")
coll = "users2"
cmd = {}
cmd['cloneCollection'] = coll
cmd['from'] = "test.example.com:27017"
db.command(cmd)
However, instead of cloning the users2 collection, it creates an empty database with the same name. Cannot figure out what I’m doing wrong. Any ideas? Thanks!
The following Ruby program is a self-contained working example that should answer your question, complete with startup of source and destination mongod servers.
It appears that the arg to cloneCollection must be a fully qualified name, e.g., “test.users” for the “users” collection in the “test” database.
Please let me know if you have any further questions.