I am trying to generate a users report which is basically a fetch of all the users in the system and add to that the total number of "orders" and "projects" each users has. This query below works perfectly when users don't have much orders/projects but if some user has lots of projects I get error:
Total size of documents in projects matching { $match: { $and: [ { owner: { $eq: ... } }, {} ] } } exceeds maximum document size
The query is:
db.getCollection('users').aggregate([
{$match: {}},
{$skip: 0},
{$limit: 50},
{$lookup: {from: 'projects', localField: '_id', foreignField: 'owner', as: 'projects'}},
{$addFields: {projects: {$size: "$projects"}}},
{$lookup: {from: 'orders', localField: '_id', foreignField: 'user', as: 'orders'}},
{$addFields: {orders: {$size: "$orders"}}}
])
Is there any workaround to that? I know I can probably use "unwind" and "group" steps but no sure how it will work with BOTH orders and projects.
MongoDB version 3.4
Thanks.