Problem:
My aggregate $graphLookup query is taking too long, specifically 5-6 minutes to find first, second and third degree connections for highly connected people like 'Superman', refer to point 2 below.
I am using the mongodb website graph example as inspiration for my schema design, mine is simpler you can view my data,schema and query here mongoplayground.net
Useful information
Borrowing from the tutorial, a bi-directional node structure connects two people to each other. One node exists from 'superman' to 'lois', another node exists from 'lois' to 'superman'.
My total collection document count is 6791, with "superman" count at 2868.
db.getCollection("people").find({from: "superman"}).count() // 2868
Every document in the collection has 3 additional fields, for brevity, I have replicated it just once in MongoPlayGround link, lines 6 - 8
I have individual indexes on fields
to
andfrom
I have a compound index on
to
andfrom
Aggregate query uses
{readPreference: 'secondaryPreferred', allowDiskUse: true}
Using mongoDB Atlas 6.0.5 M10 cluster
Collection is read often, written rarely
Open to changing the schema design