Dynamic relations in MongoDB
The pattern you describe is known as "friend of a friend." Here are a couple of answers on SO which may guide you.

In the relational database world the friends would be found by following foreign keys.  Document DBs, however, eschew explicit foreign keys enforced in the DBMS and join-like functionality in favour of other benefits. It is down to your application to recurse over successive results, even if that "makes no sense."

If you are able to expand your technology stack, a graph database would provide an excellent solution to this particular use-case. You could hold just the user-user connections to get the total list of UserIDs to whatever depth was required, then look each up in MongoDB; or you could hold the attribute values, too, and filter on like-values in the graph DB.

To speed the like-value search in MongoDB you could store a document for each attribute value, each with an array of all users who have that value.  For a given user you would then retrieve his record and the record for the given user's attribute value. The intersection of the user's friends and the attribute's user list are the friends-with-the-same-value.  Two documents only must be read rather than however many friends the given user has. Similarly on update only two documents must be written.  Sadly this does not help with finding friends-of-friends, of course.