Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9109313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:00:57+00:00 2026-06-17T03:00:57+00:00

I am trying to call the mongodb cloneCollection command from ruby. Based on the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:00:59+00:00Added an answer on June 17, 2026 at 3:00 am

    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.

    require "mongo"
    
    # startup source and destination mongod servers
    source = { 'port' => 27018, 'dbpath' => 'data27018' }
    dest = { 'port' => 27019, 'dbpath' => 'data27019' }
    [ source, dest ].each do |server|
      dbpath = server['dbpath']
      system("rm -fr #{dbpath} && mkdir #{dbpath} && mongod --port #{server['port']} --dbpath #{dbpath} &")
    end
    
    sleep 10 # delay for mongod startup
    
    db_name = 'test'
    coll_name = 'users'
    db_coll_name = "#{db_name}.#{coll_name}"
    
    # create source collection
    db = Mongo::MongoClient.new("localhost", source['port']).db(db_name)
    coll = db[coll_name]
    coll.insert({'name' => 'Gary'})
    
    # cloneCollection from source to dest
    db = Mongo::MongoClient.new("localhost", dest['port']).db(db_name)
    cmd = BSON::OrderedHash.new
    cmd['cloneCollection'] = db_coll_name
    cmd['from'] = "localhost:#{source['port']}"
    db.command(cmd)
    # verify cloneCollection
    p db[coll_name].find.to_a
    
    # kill mongod servers
    pids = `pgrep mongod`.split(/\n/).sort.slice(-2,2)
    system("kill #{pids.join(' ')}")
    

    Please let me know if you have any further questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying 'mongo-ruby-driver' for some project. It's working fine except when I call
Trying to call a SAP SOAP Web Service from a generated sudzc app shows
Im trying to call a method that will add or subtract 1 from a
I'm trying to call a method from another class with a simple button in
I'm trying to call a function present in one class from another class by
I am trying to call c# function from javascript the code i have tried
Am trying to call a JS script from a function library in another script.
Can anybody help me with this?: I'm trying call a method from a my
Im trying to call python functions from C code, and i followed a sample
Trying to call from an android client, using the signpost oauth libs. I generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.