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

141
Vistas
Mongoose - Query with $gt combined with $size not working

Here is my mongoose query (and the router):

router.get('/reportsRegular', function(req,res,next){
  Question.find({reports: {$size: {$gt: 0}}, checked: false}).sort({reports: -1}).limit(20).exec(function(err,results){
    console.log(results)
    res.render('reports', {type: 'regular', user: req.user, reports: results})
})

and it seems there is a problem with the first find condition, when I remove the $gt to 1 instead it works, but it won't work in cases with more than one, so I need to use $gt.

Here is an example JSON document that should work but does not get found:

{
  _id: new ObjectId("6212e77aa1e98ae3282a61e6"),
  title: 'TOMATOOOOOO',
  text: '<p>AAAAAAAAAAAAAAAA</p>',
  authorUsername: 'SweetWhite',
  dateCreated: 2022-02-21T01:14:34.901Z,
  answers: [],
  likes: [ 0 ],
  dislikes: [ 0 ],
  tag: 'Languages',
  views: [ 1, 'SweetWhite' ],
  reports: [ 'SweetWhite' ],
  checked: false,
  reportsNested: [],
  __v: 0
}

It should get found since the size of the reports array is bigger than zero, and the checked value is false. What am I doing wrong?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need $expr. And change to filter as:

{
  $expr: {
    $gt: [
      {
        "$size": "$reports"
      },
      0
    ]
  },
  checked: false
}

Sample Mongo Playground

about 4 years ago · Juan Pablo Isaza Denunciar
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