PRR
PRR

Reputation: 183

Write the contents of the whole mongo database (contents of every collection) into JSON file using node?

As above, trying to get a simple backup functionality to my node app and I want to find a way to write contents of every single collection in my database to one big JSON file and store it serverside. Possible?

I'm using mongoose with node and express.

Upvotes: 0

Views: 354

Answers (1)

djs
djs

Reputation: 4065

Have you tried mongodump? You just type this:

mongodump

And then it backs up all of your databases in a dump folder in the directory you ran it in. You can then restore it with mongorestore. See the backup with mongodump section of the MongoDB docs for more info.

To run from Node, you would just call mongodump from a Node file. If this was my main.js file:

// main.js
const { exec } = require("child_process");

exec("mongodump", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

Then I could run:

node main.js

And it will generate a dump folder with all my dbs in it in whatever directory I ran the command in.

Here's a gif of it on my machine:

dump

Another option, as mentioned in the comments by Joe Drumgoole, is mongoexport, which will give you the output in a human-readable format. So, if you wanted all of the documents in the users collection in your todo-api database, you would use this command:

mongoexport -v -d todo-api -c users -o 'todo.backup.json' --pretty

And that would generate a JSON file at todo.backup.json of the users collection in the todo-api database.

Here's what that looks like on my machine:

export

If you need to pull data from a MongoDB Atlas Cluster, here's the docs they provide (note that some of the info in here is related to my DB, you will need to use your own data):

Data Import and Export Tools

Replace PASSWORD with the password for the admin user, DATABASE with the name of the database you wish to import/export to your cluster, and COLLECTION with the name of the collection you wish to import/export to your cluster. Replace FILETYPE with json or csv to specify the file type. Where applicable, replace FILENAME with the location and name of the output file (for export) or data source (for import). NOTE: When exporting or importing CSV data, an additional --fields flag is often required. See documentation for the specific tool for additional details.

mongoexport | produces a JSON or CSV export of data stored in a MongoDB instance

Here's a few screenshots of where to find that info on the actual Atlas Culsters page:

Screenshot 1 Screenshot 2

mongoexport --host Atlas-Free-Cluster-shard-0/atlas-free-cluster-shard-00-00-czaoo.mongodb.net:27017,atlas-free-cluster-shard-00-01-czaoo.mongodb.net:27017,atlas-free-cluster-shard-00-02-czaoo.mongodb.net:27017 --ssl --username taskapp --password <PASSWORD> --authenticationDatabase admin --db <DATABASE> --collection <COLLECTION> --type <FILETYPE> --out <FILENAME>

Upvotes: 2

Related Questions