I need to call a function declared outside my pipeline to test some data in the query.
This is my code so far:
let requestMatcher = {
$expr: {
$function: {
body: function (modelType) {
testFunction()
return name === 'chiller'
},
args: ['$name'],
lang: 'js'
}
}
}
let pipeline = [{ $match: requestMatcher }, { $limit: 10 }]
const data = await Model.aggregate(pipeline)
const total = await Model.countDocuments(requestMatcher)
return res.status(200).send({
body: {
data,
total
}
})
testFunction() is just a console.log nothing fancy.
Aside from that testFunction(), everything else works in my pipeline.
When this code is run, I get "testFunction is not defined" which I know it's not defined inside mongo, but I would like to use some functions and variables to validate data while being queried
Am I doing something wrong here? Or $function isn't supposed to work like that?
Is there another way that I can achieve something similar with mongoose?