Unable to do mongodump or mongoexport from AWS DocumentDB instance

33 Views Asked by At

When i try to do mongodump, I am getting the below error:

Failed: error creating intents to dump: error creating intents for database <db>: error counting <db>.<col>: Aggregation stage not supported: '$collStats'

Found this update from AWS thread.

Kindly note that, the "$collStats" Stage Operator is not supported in any DocumentDB version currently. This is also mentioned in the document below

1

There are 1 best solutions below

0
Chathuranga Kasthuriarachchi On

Tried every possible way and came up with this solution.

import { MongoClient } from "mongodb";

try {
    const username = “”;
    const password = “”;
    const uri = `mongodb://${username}:${password}@host:27017/${process.env.db_name}?tls=true&retryWrites=false`;
    const sourceClient = await MongoClient.connect(uri, {
        // useNewUrlParser: true,
        // useUnifiedTopology: true,
        ssl: true,
        // sslValidate: false,
        tlsCAFile: `${__dirname}/rds-combined-ca-bundle.pem`,
    });
    const targetClient = await MongoClient.connect(“uri”, {});
    const sourceDb = sourceClient.db(“source”);
    const targetDb = targetClient.db(“target”);

    // Get all collections from the source database
    const collections = await sourceDb.listCollections().toArray();

    for (const collection of collections) {
        const colName: string = collection.name;
        console.log(`Processing collection: ${colName}`);

        // Get documents from the source collection
        const documents = await sourceDb.collection(colName).find({}).toArray();
        if (documents.length > 0) {

            // Check if the collection exists in the target database and create it if it does not
            const targetCol = await targetDb.collection(colName);

            // Insert documents into the target collection
            await targetCol.insertMany(documents);
            console.log(`Copied ${documents.length} documents to collection ${colName} in the target database`);
        }
    }
    log.info("Migration Done.");
} catch (e) {
    log.error(`Failed - ${JSON.stringify(e)}`);
}