Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
Call a js function() inside a pipeline using $function in mongoose/MongoDB

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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda