Tutorial

How To Import and Export a MongoDB Database on Ubuntu 20.04

Published on December 3, 2020
Default avatar

By Toli

English
How To Import and Export a MongoDB Database on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

Introduction

MongoDB is one of the most popular NoSQL database engines. It is famous for being scalable, powerful, reliable and easy to use. In this article we’ll show you how to import and export your MongoDB databases.

We should make clear that by import and export we mean those operations that deal with data in a human-readable format, compatible with other software products. By contrast, the backup and restore operations create or use MongoDB specific binary data, which preserve the consistency and integrity of your data and also its specific MongoDB attributes. Thus, for migration it’s usually preferable to use backup and restore, as long as the source and target systems are compatible.

Backup, restore, and migration tasks are beyond the scope of this article. For more information refer to How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04.

Prerequisites

To complete this tutorial you will need the following:

Step 1 — Importing Information Into MongoDB

To learn how importing information into MongoDB works let’s use a popular sample MongoDB database about restaurants. It’s in .json format and can be downloaded using wget like this:

  1. wget https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json

Once the download completes you should have a file called primer-dataset.json (12 MB size) in the current directory. Let’s import the data from this file into a new database called newdb and into a collection called restaurants.

Use the mongoimport command like this:

  1. sudo mongoimport --db newdb --collection restaurants --file primer-dataset.json

The result will look like this:

Output
2020-11-11T19:37:55.607+0000 connected to: mongodb://localhost/ 2020-11-11T19:37:57.841+0000 25359 document(s) imported successfully. 0 document(s) failed to import

As the above command shows, 25359 documents have been imported. Because we didn’t have a database called newdb, MongoDB created it automatically.

Let’s verify the import.

Connect to the newly created newdb database:

  1. sudo mongo newdb

You are now connected to the newdb database instance. Notice that your prompt has changed, indicating that you are connected to the database.

Count the documents in the restaurants collection with the command:

  1. db.restaurants.count()

The result will show 25359, which is the number of imported documents. For an even better check you can select the first document from the restaurants collection like this:

  1. db.restaurants.findOne()

The result will look like this:

Output
{ "_id" : ObjectId("5fac3d937f12c471b3f26733"), "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2 }, ... ], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" }

Such a detailed check could reveal problems with the documents such as their content, encoding, etc. The json format uses UTF-8 encoding and your exports and imports should be in that encoding. Have this in mind if you edit manually the json files. Otherwise, MongoDB will automatically handle it for you.

To exit the MongoDB prompt, type exit at the prompt:

  1. exit

You will be returned to the normal command line prompt as your non-root user.

Step 2 — Exporting Information From MongoDB

As we have previously mentioned, by exporting MongoDB information you can acquire a human readable text file with your data. By default, information is exported in json format but you can also export to csv (comma separated value).

To export information from MongoDB, use the command mongoexport. It allows you to export a very fine-grained export so that you can specify a database, a collection, a field, and even use a query for the export.

A simple mongoexport example would be to export the restaurants collection from the newdb database which we have previously imported. It can be done like this:

  1. sudo mongoexport --db newdb -c restaurants --out newdbexport.json

In the above command, we use --db to specify the database, -c for the collection and --out for the file in which the data will be saved.

The output of a successful mongoexport should look like this:

Output
2020-11-11T19:39:57.595+0000 connected to: mongodb://localhost/ 2020-11-11T19:39:58.619+0000 [###############.........] newdb.restaurants 16000/25359 (63.1%) 2020-11-11T19:39:58.871+0000 [########################] newdb.restaurants 25359/25359 (100.0%) 2020-11-11T19:39:58.871+0000 exported 25359 records

The above output shows that 25359 documents have been imported — the same number as of the imported ones.

In some cases you might need to export only a part of your collection. Considering the structure and content of the restaurants json file, let’s export all the restaurants which satisfy the criteria to be situated in the Bronx borough and to have Chinese cuisine. If we want to get this information directly while connected to MongoDB, connect to the database again:

  1. sudo mongo newdb

Then, use this query:

db.restaurants.find( { "borough": "Bronx", "cuisine": "Chinese" } )

The results are displayed to the terminal:

  1. Output
    2020-12-03T01:35:25.366+0000 connected to: mongodb://localhost/
  2. 2020-12-03T01:35:25.410+0000 exported 323 records

To exit the MongoDB prompt, type exit:

  1. exit

If you want to export the data from a sudo command line instead of while connected to the database, make the previous query part of the mongoexport command by specifying it for the -q argument like this:

  1. sudo mongoexport --db newdb -c restaurants -q "{\"borough\": \"Bronx\", \"cuisine\": \"Chinese\"}" --out Bronx_Chinese_retaurants.json

Note that we are escaping the double quotes with backslash (\) in the query. Similarly, you have to escape any other special characters in the query.

If the export has been successful, the result should look like this:

Output
2020-11-11T19:49:21.727+0000 connected to: mongodb://localhost/ 2020-11-11T19:49:21.765+0000 exported 323 records

The above shows that 323 records have been exported, and you can find them in the Bronx_Chinese_retaurants.json file that we specified.

Use cat and less to scan the data:

  1. cat Bronx_Chinese_retaurants.json | less

Use SPACE to page through the data:

  1. Output
    date":{"$date":"2015-01-14T00:00:00Z"},"grade":"Z","score":36}],"na{"_id":{"$oid":"5fc8402d141f5e54f9054f8d"},"address":{"building":"1236","coord":[-73.8893654,40.81376179999999],"street":"238 Spofford Ave","zipcode":"10474"},"borough":"Bronx","cuisine":"Chinese","grades":[{"date":{"$date":"2013-12-30T00:00:00Z"},"grade":"A","score":8},{"date":{"$date":"2013-01-08T00:00:00Z"},"grade":"A","score":10},{"date":{"$date":"2012-06-12T00:00:00Z"},"grade":"B","score":15}],
  2. . . .

Press q to exit. You can now import and export a MongoDB database.

Conclusion

This article has introduced you to the essentials of importing and exporting information to and from a MongoDB database. You can continue further reading on How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04.

You can also consider using replication. Replication allows you to continue running your MongoDB service uninterrupted from a slave MongoDB server while you are restoring the master one from a failure. Part of the replication is the operations log (oplog), which records all the operations that modify your data. You can use this log, just as you would use the binary log in MySQL, to restore your data after the last backup has taken place. Recall that backups usually take place during the night, and if you decide to restore a backup in the evening you will be missing all the updates since the last backup.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Toli

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel