How to import dumped Mongodb?
Asked Answered
E

4

42

Dumped a MongoDB successfully:

$ mongodump -h ourhost.com:portnumber -d db_name01 -u username -p

I need to import or export it to a testserver and have struggle with it, please help me figure out.

I tried some ways:

$ mongoimport -h host.com:port -c dbname -d dbname_test -u username -p
connected to host.
Password: ...

Gives this error:

assertion: 9997 auth failed: { errmsg: "auth fails", ok: 0.0 }

$ mongoimport -h host.com:port -d dbname_test -u username -p

Gives this error:

no collection specified!

How to specify which collection to use? What should I use for -d? What I'd like to upload or what I want to use as test out there? I would like to import the full DB not only collection of it.

Espionage answered 9/11, 2011 at 19:16 Comment(0)
F
73

The counterpart to mongodump is mongorestore (and the counterpart to mongoimport is mongoexport) -- the major difference is in the format of the files created and understood by the tools (dump and restore read and write BSON files; export and import deal with text file formats: JSON, CSV, TSV.

If you've already run mongodump, you should have a directory named dump, with a subdirectory for each database that was dumped, and a file in those directories for each collection. You can then restore this with a command like:

mongorestore -h host.com:port -d dbname_test -u username -p password dump/dbname/

Assuming that you want to put the contents of the database dbname into a new database called dbname_test.

Flight answered 9/11, 2011 at 19:24 Comment(2)
also see next answer: --authenticationDatabase admin did the trick for me.Freebooter
you might also need the archive param if it is gziped mongorestore --db database --drop --gzip --archive=archive.tar.gzTinner
P
9

For anyone else might reach this question after all these years (like I did), and if you are using

  • a dump which was created using mongodump
  • and trying to restore from a dump directory
  • and going to be using the default port 27017

All you got to do is,

mongorestore dump/

Refer to the mongorestore doc for more info. cheers!

Petta answered 3/6, 2021 at 15:41 Comment(0)
K
7

You may have to specify the authentication database

mongoimport -h localhost:27017 --authenticationDatabase admin -u user -p -d database -c collection --type csv --headerline --file awesomedata.csv 
Kagera answered 7/5, 2015 at 13:51 Comment(0)
O
5

When you do a mongodump it will dump in a binary format. You need to use mongorestore to "import" this data.

Mongoimport is for importing data that was exported using mongoexport

Omaromara answered 9/11, 2011 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.