- Published on
How to Export Multiple or All MongoDB Collections
Recently, I wanted to validate something some data from my MongoDB database locally and convert them to JSON for ease of data manipulation.
So after reading a couple of docs I tried to use mongoexport
Tried running the following command
mongoexport --uri "mongodb+srv://user:password@something.example.mongodb.net/db_name" --collection "accounts" --out accounts.json
And it did work as expected.
Then I tried various things like adding multiple collection names, leaving collection args, and also giving * as a collection.
Nothing did work.
So apparently, mongoexport can only export one collection at a time ðŸ˜
How to export multiple collections then?
Well, just export them and then merge them.
It might not be a good solution for large applications but for small projects/side projects, it should get the job done pretty well.
So here is what we do at high level:
- Get all the collection names from the DB (automatically fetched)
- Loop through all the collection manually and then get all documents from it
- Then, stitch data from all collections together and store it in a JSON file.
For the sake of simplicity, I'll be using Node.js and a related MongoDB driver. You can use any tool that you're familiar with.
// backup-all-collections.js
const { MongoClient } = require('mongodb');
const fs = require('fs');
const uri = "mongodb+srv://user:password@something.example.mongodb.net/db_name"
const client = new MongoClient(uri, { useUnifiedTopology: true });
// https://nesin.io/blog/convert-date-to-yyyy-mm-dd-javascript
const convertDate = dt => {
    const year = dt.getFullYear();
    const month = (dt.getMonth() + 1).toString().padStart(2, '0');
    const date = dt.getDate()
    return `${year}-${month}-${date}`
}
async function exportAllCollections() {
    try {
        const fileName = `${convertDate(new Date())}.json`
        await client.connect();
        const db = client.db();
        // This will fetch all the collection names from the database so you don't have to manually keep track of it.
        const collections = await db.listCollections().toArray();
        const data = {};
        for (const collectionInfo of collections) {
            const collectionName = collectionInfo.name;
            const collection = db.collection(collectionName);
            const documents = await collection.find().toArray();
            data[collectionName] = documents;
        }
        fs.writeFileSync(fileName, JSON.stringify(data, null, 2));
    } catch (error) {
        console.error('Error exporting collections:', error);
    } finally {
        await client.close();
    }
}
exportAllCollections();
Happy exporting collections!