Database

db={"posts":[{_id:1,text:"some text"}],"comments":[{_id:1,text:"comment text",postId:1}]}

Query

db.posts.aggregate([{$lookup:{from:"comments",localField:"_id",foreignField:"postId",as:"numberOfComments"}},{$addFields:{numberOfComments:{$size:{$ifNull:["$numberOfComments",[]]}}}}])

Result