databasemongodbmongock

Mongock support for executing changelogs with parameters?


I'm wondering whether Mongock has support for running a changelog multiple times with a list of parameters. My use case involves needing to insert multiple documents following the same template into one MongoDB collection, but with various values which are stored in a separate MongoDB collection.

Ideally, I'd like to write one changelog specifying the document to insert in the first collection, with an argument that I can use to set a value in the inserted document. Then, I'd like this changelog to be run multiple times for each of the values from a separate collection. Is this supported in Mongock? And if not, is there a better migration tool that would allow me to do this?

I've gone through the Mongock documentation and am not seeing any features that seem to be relevant for this...


Solution

  • Assuming your use case needs to retrieve some data from a collection (in the same or different DB), and then insert new documents in another collection using it, you can use Mongock's execution method to do that.

    If the source data is located in the same DB, you can query the collection and then use the results to generate the new documents to be inserted.

    If the source data is located in another DB, you can pass an instance of MongoDatabase / MongoTemplate for that DB as a dependency. That way Mongock's execution method can inject it as one of its method parameters.

    For further details you can take a look to this sample project, which gives you an idea of how to read data from a secondary DB and then insert it in the main DB.

    Mongock SpringBoot advance sample project

    Hope this helps you.