Database
[{"collection":"posts","count":10,"content":{"_id":{"type":"autoincrement","autoType":"int","startInt":0},"postID":{"type":"string","minLength":8,"maxLength":8,"unique":true},"title":{"type":"faker","method":"BS"}}},{"collection":"comments","count":10,"content":{"_id":{"type":"autoincrement","autoType":"int","startInt":0},"postID":{"type":"string","minLength":8,"maxLength":8,"unique":true},"comments":{"type":"array","minLength":0,"maxLength":20,"arrayContent":{"type":"object","objectContent":{"commentID":{"type":"string","minLength":4,"maxLength":4,"unique":true},"comment":{"type":"faker","method":"HackerPhrase"}}}}}}]
Query
db.posts.aggregate([{"$lookup":{"from":"comments","let":{"postId":"$postId"},"pipeline":[{"$match":{"$expr":{"$eq":["$postId","$$postId"]}}},{"$group":{"_id":"$postId","comments_count":{"$sum":1}}}],"as":"comments"}},{"$project":{"_id":0,"postId":1,"title":1,"comments_count":"$comments.comments_count"}}])