How do I export/dump mongodb database?
Asked Answered
S

2

25

I tried a mongo export like this:

./mongodump --db local --collection lecturer 

and then I tried:

./mongodump --db  local --collection posts --out - >  lecturer .csv  

and I get the same error message: Syntax Error: syntax error (shell):1

  1. What's wrong with my code?
  2. Where is the data stored if export successfully?
Soloman answered 14/6, 2012 at 21:28 Comment(1)
You should accept helpful answers, by the way.Ridglee
K
48

How to backup and restore databases

Start Mongo, open a new tab in terminal. First navigate to the folder where you want to save the backup, then type the following command.

Backup single database:

mongodump --host localhost --port 27017 --db db_name

Restore single database:

mongorestore --host localhost --port 27017 --db **** dump/db_name

(In this case, **** represents UserDefinedName for the database > mydb dump/db_name > this will import dump db into mydb)

Backup all databases:

mongodump --host localhost --port 27017

Restore all databases:

mongorestore --host localhost --port 27017  dump
Kauai answered 25/6, 2014 at 7:17 Comment(1)
this comment is to help to others, from mongodb 4.4+ you need install mongodb-tools to get mongodump/mongorestore working.Undergraduate
R
28

mongodump is a command-line utility and it's supposed to be run from the system command prompt, not the mongo javascript shell.

./mongodump --db local --collection lecturer

if successful, this command will create some files under dump directory in the current dir.

Ridglee answered 14/6, 2012 at 21:30 Comment(1)
for the mongo clueless like me who came here, replace local with the name of the DB you want to export and leave off --collection if you want to export the whole db.Josephson

© 2022 - 2024 — McMap. All rights reserved.