MongoDB - Import and Export
In the last guide, we talked about GridFS—how to store files in MongoDB. In this article we’ll cover importing and exporting MongoDB from one machine to another. There are a few reasons to import and export. Sometimes is part of the deployment, or in other words moving from one environment to another. Or sometimes it could be for backing up.
In MongoDB, like in MySQL, we need to use third party tools in order to import/export. All the examples in this article are in Linux, but if you saw the last MongoDB article I mentioned how all the examples in linux can be made to run in Windows. How? Just open the cmd, go to the folder where you put MongoDB, and run the Linux example with .exe added. If for example the command in Linux is mongorestore, in Windows it will be mongorestore.exe. Simple enough, no?
Actually, the easiest thing to do is just work with Linux. If you’re not working with .NET, then there’s really no reason to not be running Linux. It’s easy and you can do it with a virtual machine.
Now that my brief rant is over, how do we export? Pretty easily actually. We have two ways of carrying out export. The first and more reliable is mongodump which creates JSON and BSON files for us. Sounds good but there’s a slight problem. JSON files contain non-standard data and therefore other systems (not MongoDB) can have difficulties dealing with them. But if you want to transfer from Mongo to Mongo then this is the method.
If you want clean JSON or CSV, you’ll need to use mongorestore which is very similar to using mongodump, but it might make files that are slightly different then the ones on your system, especially if you have unique data.
How do we use it? Just write mongodump and it will dump out all the databases in the dump folder from wherever you ran it. After the export, you’ll see that in the folder there are now many folders(one for each database) and in each folder all the collection files, which are BSON and JSON files. The BSON are binary and the others are JSON.
And if we want to export just one database and not all of them? No problem:
mongorestore -d test
The line of code above will only export the database called test. Hold on, but what if I want just a collection?
mongodump -d test -c pages
OK, but what if I don’t want the entire collection but just specific documents that meet certain conditions? For instance, just those whose title ends in ”61″.
mongodump -d test -c pages -q '{"title":/61$/}'
Hold on, hold on! What if I want to set the specific file that I’ll receive? And I want it to clean the entire house, and give me a massage?
All this and more you can learn about with mongodump –help or by having a look at the mongodump documentation. Though I wouldn’t bank on the whole clean the house/massage thing.
Now that we’ve exported, it’s time to import! So how do we do this? It’s quite simple: run mongorestore in the same place with the dump folder. If you’re not in the place where the dump folder is, then you can do something like this:
mongorestore www/dump/test/
There are more than a few possibilities with mongorestore. And where do we find them? I think you already know. Either in the mongorestore documentation or simply type mongorestore –help.
Pretty fun, huh?
By the way, we should note here that using import/export slowes MongoDB to a crawl in terms of performance. So if you’re going to use it on a live server, it would be best to check the implications more in depth than what we’ve covered here. Don’t blame me if your application comes grinding to a halt.
In the next article we’ll examine using MongoDB with PHP.
About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.
Recent Stories
Top DiscoverSDK Experts
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment