How to use mongodump for 1 collection
Asked Answered
T

6

98

How can I use mongodump to move a single collection from one database to another?

How should I use the command and its options?

Trumpery answered 2/5, 2013 at 20:28 Comment(0)
C
173

I think it's just:

mongodump --db=<old_db_name> --collection=<collection_name> --out=data/

mongorestore --db=<new_db_name> --collection=<collection_name> data/<db_name>/<collection_name>.bson

Also see docs here and here.

Btw, the other way to move the collection from one database to another is to use renameCollection:

db.runCommand({renameCollection:"<old_db_name>.<collection_name>",to:"<new_db_name>.<collection_name>"})

Here's some related SO threads:

Confess answered 2/5, 2013 at 22:9 Comment(4)
thank you, its giving me following error:Error: missing ; before statement (MongoSession#1) for first command you gave.Trumpery
this is the command I am Trying: mongodump --db=DevDB --collection=Functions_List --out=data/Trumpery
you need to run the first command from the OS prompt, not mongo shell.Handsome
For those who are dealing with replica sets, see this answer..Mallemuck
U
30

Taking database (document) dump (backup)

mongodump --host <hostname-of-mongoserver> --db <db-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>

Taking collection dump (backup)

mongodump --host <hostname-of-mongoserver> --db <db-name> --collection <collection-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>

mongodump documentation

Unconditional answered 2/11, 2016 at 17:36 Comment(1)
For anyone who need to restore: mongorestore --gzip --db <db-name> <collection-name.bson.gz>Barnie
P
7

Very basic commands for dump mongodb.

  1. Dump all collection

    mongodump
    
  2. Dump specific database only

    mongodump --db=DB_NAME
    
  3. Dump database with username & password

    mongodump -u=USERNAME -p=PASSWORD --db=DB_NAME
    
  4. Dump from another host

    mongodump --host HOST_NAME/HOST_IP --port HOST_PORT  --out {YOUR_DIRECTOTY_PATH} --db=DB_NAME
    

Only able to dump from another host when they allow it.

Portray answered 27/10, 2016 at 6:43 Comment(0)
B
4

If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri

mongodump --uri "mongodb://user:[email protected]:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin"  --collection $collectionname

Then restore it the usual way.

Barbabra answered 8/3, 2021 at 14:59 Comment(1)
This is the way to go, if it is a replica set that you are dealing with...Mallemuck
K
1

Here is an example of how you can export a single collection with mongodump.exe on Windows 10:

"D:\Program Files\MongoDB\Server\4.0\bin\mongodump.exe" -h localhost --port 27017 -d meteor --collection users -o meteor_users

The exported collection is users, the database is meteor, the host localhost, the port is 27017.

The output will be stored in directory meteor_users.

Restoring should use a command like this one:

"D:\Program Files\MongoDB\Server\4.0\bin\mongorestore.exe" -d meteor -c users users.bson
Kharif answered 20/11, 2020 at 9:13 Comment(0)
T
1

None of them works for me while doing dump for MongoDB atlas. Here is the minor change in the host that work for me

Dump

mongodump --uri mongodb+srv://<USERNAME>:<PASSWORD>@host.abcd.mongodb.net/db_name --collection "user_collection" --gzip --out db_backup_folder

Restore

mongorestore --uri mongodb+srv://<USERNAME>:<PASSWORD>@dbhost.abcd.mongodb.net -d db_name --gzip  db_backup_folder

atlas-database-tools-backup-restore

Toccaratoccata answered 11/8, 2022 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.