How to create a stored js file and invoke it from spring boot application

Here is my situation. I have audit documents and over time it will be growing in size. Our design requirement was to query audit documents and update those results to another document called ticket which will be created before querying documents. The catch here was invoking js query happens from a spring boot application but after invocation the connection from java to mongo should be closed and the query execution should happen in mongo db as this query execution can take from few mins to hours.

I was creating a function in mongo db but as per this link(Alternative of eval() command in Java) my requirement is only possible through mongo shell and can’t be fulfilled with java mongo driver.

Any help will be greatly appreciated

function test() {
db = connect(“xxxx:xxxxxxx@yyyyyyyy:zzzzz/qqqqq”);
const res = db.getCollection(‘audittt’).find( {
$and: [
{“userId”: {$eq: “fdfdf”}},
{“businessFunction”: {$eq: “sdff”}},
{“applicationName”: {$eq: “HdfdfRS”}},
{"$gte":[{"$convert":{“input”:"$_id",“to”:“date”}}, ISODate(“2022-07-01T00:00:00.000Z”)]},
{"$lte":[{"$convert":{“input”:"$_id",“to”:“date”}}, ISODate(“2022-07-06T11:59:59.999Z”)]}
db.audit.update({ticketNumber: “4O7nv2CFAq”}, {$set : {result : res.toArray()}});


I have a function like this and i would like to invoke this function from a spring boot application. can i do it and how can i do it. any help will be appreciated