TECHEVANGELISTSEO – WORDPRESS SEO IPHONE TIPS How Tos

  • Home
  • seo
  • Tech News
    • Apple Google Microsoft
      Apple

      FaceTime: The ultimate guide

      November 6, 2020

      Apple

      imei iPhone check for iCloud check, iPhone blacklist…

      October 24, 2020

      Apple

      iPhone carrier checker free ( Gsx Checker) 100%…

      October 23, 2020

      Apple

      iPhone X 6 Months Later: Still One Of…

      May 27, 2018

  • Link Building Service
  • iCloud unlock Service
  • Gaming
6.7 C
New York
  • Sign in / Join

Login

Forgot your password?
X

Register

Have an account?Login here
X
TECHEVANGELISTSEO – WORDPRESS SEO IPHONE TIPS How Tos
Tech News

How To Import and Export a MongoDB Database on Ubuntu 20.04

by abihordunDecember 3, 2020012
Share0

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:

  • One Ubuntu 20.04 Droplet set up per the Ubuntu 20.04 initial server setup guide, including a sudo non-root user and a firewall.
  • MongoDB installed and configured using the article How to Install MongoDB on Ubuntu 20.04.
  • An understanding of the differences between JSON and BSON data in MongoDB. For a detailed discussion read Step One — Using JSON and BSON in MongoDB in our tutorial, How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04.

Step One — 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:

  • 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:

  • 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.

Related Posts:

  • Today-only sale on OnePlus 8 and more of the best phone dealsToday-only sale on OnePlus 8 and more of the best…
  • Save $330 on a Samsung Galaxy Tab S6 and more of the best tablet dealsSave $330 on a Samsung Galaxy Tab S6 and more of the…
  • How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04How To Back Up, Restore, and Migrate a MongoDB…
  • How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 18.04How To Back Up, Restore, and Migrate a MongoDB…
  • How To Back Up, Restore, and Migrate a MongoDB Database on CentOS 8How To Back Up, Restore, and Migrate a MongoDB…

Powered by Contextual Related Posts

Let’s verify the import.

Connect to the newly created newdb database:

  • 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:

  • 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:

  • db.restaurants.findOne()

The result will look like this:

[secondary label 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:

  • exit

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

Step Two — 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:

  • 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:

  • sudo mongo newdb

Then, use this query:

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

The results are displayed to the terminal:

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

To exit the MongoDB prompt, type exit:

  • 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:

  • 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:

  • cat Bronx_Chinese_retaurants.json | less

Use SPACE to page through the data:

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}],
  • . . .

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.

Related

Related posts:

  1. October 2020 security update reaches the Galaxy Tab S7 series worldwide
  2. Galaxy Z Fold 3 might have the most intricate design with a pop-up camera
  3. Pre-order the Galaxy Z Fold 2 in Brazil and get the latest wearables for free
  4. Samsung to ship the Galaxy S21 series with One UI 3.1, rumor claims
samsung
Share0
previous post
The Galaxy Watch 3 is now even cheaper than it was on Black Friday
next post
Possible Galaxy S20 FE, Note 20 series One UI 3.0 release dates revealed
abihordun

Related posts

This is Samsung’s 2021 budget phone, the Galaxy M12 (F12), in renders

abihordunNovember 16, 2020

Watch a dramatic rocket launch shot on iPhone 12 Pro lift off in slow-motion [Video]

abihordunNovember 14, 2020

Samsung Galaxy S21, S21 Plus Full Specifications Leaked, Price Expected to Start at Approx Rs 76,800

abihordunDecember 27, 2020December 27, 2020

Archives

  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • May 2018

Categories

  • Android
  • Apple
  • Applications
  • Content Marketing
  • Editor's Picks
  • Entertainment
  • Featured
  • Gadgets
  • Gaming
  • Google
  • Inbound Marketing
  • iPhone
  • Microsoft
  • Mobile Search
  • Reviews
  • Science
  • seo
  • SEO 101
  • Social Media
  • Tech News
  • Tips and Ticks
  • Uncategorized
  • wordpress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Recent Posts

  • 9 Best Online Course Platforms for 2021 (Compared) January 18, 2021
  • What is an XML Sitemap? How to Create a Sitemap in WordPress? January 18, 2021
  • How to Track User Journey on WordPress Lead Forms January 18, 2021
  • Best Buy 4-day sale discounts MacBook Pro, smart home accessories, more January 18, 2021
  • Galaxy S21 seems impressively repairable in this early teardown January 18, 2021

Recent Posts

  • 9 Best Online Course Platforms for 2021 (Compared)
  • What is an XML Sitemap? How to Create a Sitemap in WordPress?
  • How to Track User Journey on WordPress Lead Forms
  • Best Buy 4-day sale discounts MacBook Pro, smart home accessories, more
  • Galaxy S21 seems impressively repairable in this early teardown

Recent Comments

    Newsletter

    Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

    Categories

    • Android (6)
    • Apple (14)
    • Applications (6)
    • Content Marketing (10)
    • Editor's Picks (14)
    • Entertainment (6)
    • Featured (6)
    • Gadgets (6)
    • Gaming (64)
    • Google (11)
    • Inbound Marketing (7)
    • iPhone (6)
    • Microsoft (5)
    • Mobile Search (8)
    • Reviews (6)
    • Science (6)
    • seo (261)
    • SEO 101 (10)
    • Social Media (8)
    • Tech News (1,106)
    • Tips and Ticks (247)
    • Uncategorized (13)
    • wordpress (124)
    TECHEVANGELISTSEO – WORDPRESS SEO IPHONE TIPS How Tos
    About US
    PenNew is a WordPress Theme for News & Magazine, designed and developed by PenciDesign. This is a powerful theme with tons of options, which help you easily create/edit your Websites in minutes. You can use this WordPress Theme for every purposes to meet your need.
    Contact us: [email protected]
    Follow us
    FacebookTwitterInstagramGoogleYoutube
    ©2021 - techevangelistseo.com. All Right Reserved.
    • About
    • Privacy-policy
    • Disclaimer
    • Affiliate Link Policy
    • Contact
    • TECHEVANGELISTSEO
    • iCloud unlock Service
    • Link Building Service
    • Privacy Policy
    TECHEVANGELISTSEO – WORDPRESS SEO IPHONE TIPS How Tos
    FacebookTwitterInstagramGoogleYoutube
    • Home
    • seo
    • Tech News
      • Apple
      • Google
      • Microsoft
      • Samsung
    • Link Building Service
    • iCloud unlock Service
    • Gaming

    Add TECHEVANGELISTSEO | SEO Tips With YouTube Videos Infographic And How To to your Homescreen!

    Add