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","localField":"postID","foreignField":"postID","pipeline":[{"$project":{"_id":0,"commentCount":{"$size":"$comments"}}}],"as":"commentCount"}},{"$project":{"_id":0,"postID":1,"title":1,"commentCount":{"$first":"$commentCount.commentCount"}}}])