Tutorial on exporting mongodb database with mongoexport
In our getting started with mongodb tutorial, we created a database in mongolab called “firstdb” with a “people” collection. Let’s export that collection into JSON format using the mongoexport utility that comes with mongodb shell.
1. First we need to figure out some information about our database. Log into mongolab and go into the database and then click “Tools -> import/export” to determine the hostname, port, and database. They even gave us the mongoexport command …
2. In the command string, replace <collection> with “people” collection. Replace <user> with the database user. And replace <password> with the corresponding user password.
The -h flag indicates that the hostname follows it. The -d flag specifies the name of the database — in our case it is “firstdb”. You can learn more about the mongoexport options and flags here.
3. To determine the user to use, click on the “users” tab in mongolab. It will show you the username, but not the password. If you forgot the password, you have to create a new user for this database.
4. Now that we have all the information for our command string. Open up an shell window to the mongodb directory and type it in …
Note that we had put in “people.json” as the <output file>
5. See that two records was exported if you open up people.json in a text editor.
Note: mongoexport is not intended to backup a full production database. Use mongodump instead. As noted in the documentation …
“Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.”