DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Importing and Exporting MongoDB Databases

Importing and Exporting MongoDB Databases

Lorna Mitchell user avatar by
Lorna Mitchell
·
Dec. 08, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
6.10K Views

Join the DZone community and get the full member experience.

Join For Free

I'm enjoying working with MongoDB but as with any new technology, it can take a little while to find your way around all the tools related to that stack. In particular, I found myself wondering how do I mysqldump for mongodb?

It should have come as no surprise that the command I wanted was called mongodump, really! There are lots of ways of importing/exporting mongodb databases, and there is some excellent documentation on the mongodb.org site about the various tools.

I moved one database from one server to another, and it was pretty straightforward so I thought I'd write down the commands that I used so I can refer to them later (I did this yesterday and already I've had to look at my bash history to remember how to do it!).

Exporting from MongoDB

To export the database, simply tell mongodump which database (or collection) you want to export, and where to export it to. Mine was the pets database, so my command looks like this:

mongodump -d pets -o petsbackup

This dumps the pets database into the petsbackup directory. Take a look at what we have in that directory now:

pets
├── animals.bson
└── system.indexes.bson

0 directories, 2 files

The only collection in my pets database is the animals collection, however you'll see a .bson file for each collection in your database, plus the system indexes collection. It is up to you whether you want to take individual collections, or a whole database, but bear in mind that your choice will dictate whether you get information about indexes etc when you import the data elsewhere.

Importing to MongoDB

To import, simply use the mongorestore command, which accepts either a single .bson file representing a collection, or a directory containing multiple files. Here's my example:

mongorestore -d pets /path/to/pets

You can specify any database name and path to files you like, so for taking backups or restoring additional copies of a database, this can be really handy. The mongo commands are well-documented and I found them easy to work with - hopefully this helps you work with them too!

 

From http://www.lornajane.net/posts/2011/importing-and-exporting-mongodb-databases

Database MongoDB

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Version Number Anti-Patterns
  • Introduction to Word Vectors
  • Correlation Between Fast TTM and Containers
  • Python 101: Equality vs. Identity

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo