lihaibh / ditto

NPM library that helps managing MongoDB snapshots easily and efficiently
https://www.npmjs.com/package/mongodb-snapshot
MIT License
50 stars 6 forks source link

Can you add upsert mode to replace existing documents? #11

Closed truongtv22 closed 3 years ago

truongtv22 commented 3 years ago

It's like mongoimport 😃

lihaibh commented 3 years ago

basically you configure a target connector with a boolean to determine whether or not you want to perform cleanup before the trasfer process begins. so if you do not perform cleanup it will just add more documents to it. but in case of a duplicated key you will receive an error.

for example:

async function copyMongo2Mongo() {
    const mongo_connector_1 = new MongoDBDuplexConnector({
        connection: {
            uri: `mongodb://<username>:<password>@<hostname>:<port>`,
            dbname: '<database-name>',
        },
    });

    const mongo_connector_2 = new MongoDBDuplexConnector({
        connection: {
            uri: `mongodb://<username>:<password>@<hostname>:<port>`,
            dbname: '<database-name>',
        },
       astarget: {
             remove_on_startup: false
        }
    });

    const transferer = new MongoTransferer({
        source: mongo_connector_1,
        targets: [mongo_connector_2, mongo_connector_3],
    });

    for await (const { total, write } of transferer) {
        console.log(`remaining bytes to write: ${total - write}`);
    }
}

can you open a Pull Request?

maybe when "remove_on_startup: false" for mongodb connector we can use upsert instead of normal insert.

lihaibh commented 3 years ago

@truongtv22 are you working on a pull request or can you give more details of what you are trying to achieve? this issue is open for almost a month without any progress.