Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Move a collection from one DB to another DB within the same MongoDB instance

I have many collections in database A. I want to copy some of them to database B in the same MongoDB.

I tried to copy whole database using db.copyDatabase('A', 'B'), but it has more than 300 GB data. It will take ages to copy and I just want to copy a few collections from database A to database B.

Does anyone know how can I do it?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can try it using mongo shell. you can copy collection from one db to another db using renameCollection. the renameCollection can run from admin database so first need to switch to admin db.

so ca follow bellow steps in mongo shell:

step-1: run this comment use admin

step-2: run bellow comment

db.runCommand({renameCollection:"sourcedb.sourceCollection",to:"targetdb.tragetCollection"})

for example:

use admin
db.runCommand({renameCollection:"funnel.countries",to:"test.countries"})

copied countries collection from funnel db to test db.

In background MongoDB will create dump for source collection and restore the dump automatically to target db collection

over 4 years ago · Santiago Trujillo Report

0

Use mongodump to dump the collections:

mongodump  --db A --collection coll --out yourbackupdir

and then import the collections using mongorestore:

mongorestore --db B --collection coll yourbackupdir/
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!