MongoDB extract subdocument array values from multiple documents

Viewed 190

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?

1 Answers
Related