A graph database is probably a better tool for this application than MongoDB. The simplest way to do it with MongoDB would take 1+N queries:
# Get a cursor for the entire collection
docs = db.collection.find()
for doc in docs:
# Get all documents that have a common element
related_docs = db.collection.find({"$or": [
{"color": doc["color"]},
{"name": doc["name"]},
{"age": doc["age"]},
]})
# Record the relationships in whatever structure you're using
for related_doc in related_docs:
store_relationship(doc, related_doc)
You could make this more efficient by keeping track of which document pairs you've already seen and ignoring repeats. As written you'll see each edge twice.