Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!