Press "Enter" to skip to content

Abhay PS Posts

MongoDB backup and restore

Backup and restore are important routine jobs. If you are working with MongoDB then you must know how to take backups as well as how to restore them. MongoDB comes with wonderful command line tools for backup and restore, i.e. mongodump and mongorestore.

I will not be explaining all the backup/restore method here; you can refer MongoDB documentation for backup and restore strategies for more insight. But what I am going to show you are, the most common and simple ways to backup and restore MongoDB.

Copying data Files/Folder
The simplest way to backup or restore MongoDB would be to simply copy the database folder/files. That means if you are taking backup then copy the database folder/files to your backup location. And if you are restoring then copy the content of your backup folder to the database folder.

CRUD operations in MongoDB

The acronym ‘CRUD’ stands for Create, Read, Update and Delete. In this section, we will learn basic CRUD operations. CRUD operations can be extremely complex sometimes but we will not go into the complex stuff for two reasons. First, it is not under the scope of this chapter. Second, we will be doing a lot of CRUD operations in the following chapters.

Before we start learning CRUD, I must tell you that we will be using mongo shell for CRUD in the following sections.

Create or insert document
Data can be added to MongoDB in the form of documents. To add document, we use the insert function. We pass the document to be inserted to insert() function. Let’s see an example –

Mongodb GridFS using Python

Data is collected and stored in different forms depending on the nature of data. It can be in the form of text, images, videos, audio files and files in other formats. MongoDB can be used for storing all kinds of data, but so far we have used it for storing plain text information in MongoDB documents. As you know by now that MongoDB document has a size limit of 16MB. Though 16MB is good enough in most cases but looks tiny if you think of storing high-resolution images, PDF files, music, videos etc.

In this chapter, we are going to see how we can store more information in MongoDB. Till now we played around with storing text in JSON format but in this chapter we’ll see how to store images, PDFs, audio, video etc. I am sure you are going to enjoy this chapter. So, let’s get started!