MongoDB fastest way to aggregate stats from referenced collection

Viewed 18

So I have two collections, one is Users, (100-200) items:

{
name: string
// ...
}

Another is WorkItems, which is large (200k):

{
user: ObjectId
name: string
price: number
start: date
finish: date
}

I need to get all users with some stats of works, applying filter for workItems preview (for example price lower then 50):

{ 
name: 'UserName',
minPrice: 0, // min price of ALL user WorkItems
maxPrice: 100, // max price of ALL user WorkItems
start: '...', // earliest date of ALL user WorkItems
finish: '...', // latest date of ALL user WorkItems
items: [] // 3 user work items by specified filter
}

What is the most effective request or solution (maybe some kind of indexes for fast computing of stats, or some kind of calculated fields) for this kind of data? Aggregating WorkItems and lookup of Users is slow, I suppose because we need whole collection to fill the stats.

0 Answers
Related