'Executing arbitrary js code with liquibase-mongodb?

I would like to try using liquibase-mongodb for automatic migrations of Mongo database.

My migration is quite complex (uses variables, control flow, etc), so it looks like a single runCommand is not enough. I need to execute mongosh's js code.

Mongo version used by the app is 4.4. I know already, that Mongo's server-side eval was deprecated since 3.0 and dropped since 4.2. This fact initially gave me an impression, that it's not possible to execute plain js migrations, but there is some evidence that it is still possible:

  • there is an src/test/resources/liquibase/ext/js folder with serveral js migrations, but I haven't found how they are referenced to get executed
  • there is a #97 discussion on GitHub issues, which says that liquibase-mongodb is an alternative to deprecated Mongo's eval, but I still can't understand how can I execute arbitrary js. Is a sequence of runCommands the only alternative? If so, how can I take output from one runCommand and use it as an input of another runCommand?
  • If I connect to my Mongo 4.4 with DataGrip/Intellij db module, then I am able to use variables in db console (the same way I can do with mongosh executing js statements one by one)

So, the question is: how to execute a bunch of Js statements within a single liquibase-mongodb migration? If it's possible with DataGrip console or mongosh, then I guess it should be possible with liquibase-mongodb (even though server-side's eval does not work anymore)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source