I have collections Users and Numbers. Each number has AllocatedToUserId (ObjectId) field, User can have many numbers. How can I in single query return User with NumbersCount? I wrote this
db.getCollection('Users').aggregate([
{'$lookup': {from: "Numbers",
localField: "_id",
foreignField: "AllocatedToUserId",
as: "Numbers"}
},{ '$project' : {_id:0,
document: '$$ROOT',
count: {$size:'$Numbers'}}}
])
But with this code I got structure of response like that:
[{document, count},{document, count},{document, count}...]
Where each document has Numbers collection, and I'd like to have count inside document and without collection of numbers. Is it possible? Thanks!