Database
db={"categories":[{"id":"A1001","key":"3dmodels",},{"id":"A1002","key":"animals",},{"id":"A1003","key":"birds",},{"id":"A1004","key":"reptiles",},{"id":"A1005","key":"birds",}],categories_ref:[{"category_id":"A1001",/** 3dmodels parented to*/"p_category_id":"root",/** root*/},{"category_id":"A1002",/** animals parented to*/"p_category_id":"A1001",/** 3dmodels*/},{"category_id":"A1003",/** birds parented to*/"p_category_id":"A1002",/** animals*/},{"category_id":"A1004",/** reptiles parented to*/"p_category_id":"A1002",/** animals*/},{"category_id":"A1005",/** birds parented to*/"p_category_id":"A1004",/** reptiles*/}]}
Query
db.categories.aggregate([{"$match":{"key":"birds"}},{"$lookup":{"from":"categories_ref","let":{"id":"$id"},"pipeline":[{"$match":{"$expr":{"$and":[{"$eq":["$category_id","$$id"]},{"$eq":["$p_category_id","A1002"]}]}}}],"as":"cat_ref"}},{"$match":{"cat_ref":{"$ne":[]}}},{"$project":{"cat_ref":0}}])