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

133
Visualizações
Find documents having a field greater than the length of an embeded array

Below is a group schema:

GroupSchema{
    membersLimit: Number,
    listOfStudents: [String]
}

I want to find all the groups where the number of students is less than membersLimit.

I tried the folliowing query

await Group.find({
    membersLimit: { $gt: 'listOfStudents'.length },
})

This query returns documents having students less than 14 i.e. length of listOfStudents.

I also tried this

await Group.find({
    listOfStudents: { $size: { $lt: 'membersLimit' } },
})

This query returns the following error

error The expression evaluated to a falsy value:

  assert.ok(!isNaN(val))

Is there a way to do this with single query?

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to use $expr, to compare the array size and the field, like this:

db.collection.find({
  $expr: {
    $gt: [
      "$membersLimit",
      {
        "$size": "$listOfStudents"
      }
    ]
  }
})

Playground link.

almost 4 years ago · Santiago Trujillo Relatório
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