Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

347
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda