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

280
Visualizações
MongoDB extract subdocument array values from multiple documents

I am using Bucket Pattern to divide a time series of market updates and avoid exceeding document size (transfer overhead is not a concern). Each document has 30,000 updates and I need to return those updates from all documents matching 'marketId' in an array (hopefully sorted).

Data:

{
  _id: 60617172eca858909eace71f,
  marketId: '1.278363651',
  eventId: 5697224,
  marketType: 'OVER_UNDER_15',
  size: 30000,
  updates: [ 
  {
      t: 1616998770482.0,
      p: 36.49
  }, 
  {
      t: 1616998770482,
      p: 87.77
  },
  // ... 29998 more
  ]
},

Desired outcome:

[
  { t: 1616998770482, p: 36.49 },
  { t: 1616998770482, p: 16.59 },
  { t: 1616998770482, p: 40.38 },
  ... // sorted by t
}

This my the closest attempt with an aggregation:

const result = await db.collection('markets').aggregate(
  { $match: { marketId: marketId } },
  { $project: { _id: 0, updates: 1 } },
  { $unwind: "$updates" },
  { $unwind: "$updates" },
).toArray();

Output:

[
  { updates: { t: 1616998770482, p: 36.49 } },
  { updates: { t: 1616998770482, p: 16.59 } },
  { updates: { t: 1616998770482, p: 40.38 } },
  ... // actually gives me all of them
}

How can I remove the "updates" and get to the actual object?

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

0

Demo - https://mongoplayground.net/p/Dw7NAn9EoEd

Use $replaceRoot

db.collection.aggregate([
  { $match: { marketId: marketId } },
  { $project: { _id: 0, updates: 1 }},
  { $unwind: "$updates" },
  { $replaceRoot: { newRoot: "$updates" } }
])
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