How to export data from firebase firestore database in json format using node

Malcolm Maima
3 min readJan 6, 2023

--

Firebase Firestore is a NoSQL document database that allows you to easily store, sync, and query data for your mobile and web applications. It is a flexible, scalable database that can store and sync data for your users in realtime.

In this tutorial, we will learn how to export data from Firestore in JSON format using the node-firestore-import-export library and the firestore-export command.

Prerequisites

Before you begin, make sure you have the following:

  • A Firebase project and a Firestore database set up. If you don’t have one, you can create one by following the Firebase documentation.
  • The node-firestore-import-export library installed on your machine. You can install it by running npm install -g node-firestore-import-export.

Exporting data from Firestore

To export data from your Firestore database, follow these steps:

  1. Open the Firebase Console.
  2. Navigate to your project and click the gear icon next to the “Project Overview” heading.
  3. Click the “Project Settings” button.
  4. In the “Service accounts” tab, click the “Generate new private key” button. This will download a JSON file with your Firebase project’s configuration.
  5. Rename the downloaded file to appConfig.json.
  6. Open a terminal and navigate to the directory where you want to save the exported JSON file. Make sure the above file you just renamed exists in the same directory as well.
  7. Run the following command to export data from your Firestore database:
npx -p node-firestore-import-export firestore-export -a appConfig.json -b backup.json

This command exports all data from your Firestore database and saves it to a JSON file named backup.json in the current directory.

Options

The firestore-export command has several options that you can use to customize the export process. Here are some of the most commonly used options:

  • -a, --appConfig: Specifies the path to the JSON file that contains your Firebase project's configuration. If you don't specify this option, the command will look for a file named appConfig.json in the current directory.
  • -b, --backup: Specifies the path to the JSON file where the exported data will be saved. If you don't specify this option, the command will create a file named backup.json in the current directory.
  • -c, --collection: Specifies the name of a specific collection to export. If you don't specify this option, the command will export all collections in the database.
  • -h, --help: Displays the command's usage and options.
  • -l, --limit: Specifies the maximum number of documents to export from each collection.
  • -p, --pretty: Formats the JSON output in a more human-readable format.

Conclusion

In this tutorial, we learned how to export data from Firestore in JSON format using the node-firestore-import-export library and the firestore-export command. We also learned about some of the options that you can use to customize the export process

In conclusion, exporting data from Firebase Firestore in JSON format is a straightforward process using the node-firestore-import-export library and the firestore-export command. The process involves installing the library, generating a private key file for your Firebase project, and running the firestore-export command with the necessary options.

It is important to note that the firestore-export command exports all data from your Firestore database by default. If you want to export data from a specific collection or a limited number of documents, you can use the -c or --collection option to specify the collection name and the -l or --limit option to set the maximum number of documents to export.

Exporting data from Firestore can be useful for backing up your data, migrating it to a different database, or simply for archiving purposes. It is a good practice to regularly export and backup your data to ensure that it is not lost in case of any unforeseen circumstances.

I hope this tutorial was helpful and that you are now able to export data from your Firestore database with ease.

--

--

Malcolm Maima
Malcolm Maima

Written by Malcolm Maima

Android Engineer @ Equity Bank — I code stuff…I eat a lot too, coffee makes me sleepy Love cartoons and robots. http://linktr.ee/malcolmmaima

No responses yet