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

738
Visualizações
Why mongo doesn't allow to use a $facet stage inside another $facet?

There is $facet aggregation stage in mongo since 3.4 - It's cool. It allows to processes multiple aggregation pipelines within a single stage on the same set of input documents.

But it doesn't allow to use one $facet inside another. Cite: "Any other aggregation stages can also be used with $facet except: $facet, $out, $geoNear, $indexStats, $collStats"

Does anyone understand the reason?

I just want to use $facet this way:

db.collection.aggregate([{
  $facet: {
    'first': [
      $facet: { // here is the sub $facet
         'subFirst1': [],
         'subFirst2': []
      }   
    ],
    'second': [
      //...
    ]
  }
}])

This should produce a doc like:

{
  first: {
    subFirst1: {...},
    subFirst2: {...}
  },
  second: {...}
}

If I try this it throws the error:

specified stage is not allowed to be used within a $facet stage: 0: { $facet: { subFirst1: [ ... ] } }"
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's not only about facets. In general it's not allowed to create subfields in an aggregation stage. For example the following attempt of creating a complex value fails.

$addField: {
  a.b: "foo"
}

And that makes quite sense, since a value can be a primitive type, a complex object or an array. Don't know if that is a satisfying answer to you, maybe it's a little consolation that this not only counts for facets, but for all assignments in general.

I want also say that your intention is plausible. Especially in a database system, where no schema is required and each document can differ from others, it would be nice to have that feature. If you actually manipulate data you can do so with set. But on your intermediate results, you are not allowed to perform that action. One can perceive that as a bit inconsistent.

over 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