I have a collection of "who is following who" (like Instagram):
db.users.insertMany([
{ _id: 1, name: "Arnold Schwarzenegger" },
{ _id: 2, name: "James Earl Jones" },
{ _id: 3, name: "Harrison Ford" },
{ _id: 4, name: "Jennifer Lawrence" }
]);
db.follows.insertMany([
{ _id: 12, follower: 1, following: 2 },
{ _id: 13, follower: 1, following: 3 },
{ _id: 24, follower: 2, following: 4 },
{ _id: 23, follower: 2, following: 3 }
]);
I'm trying to suggest other users whom others they can follow. i.e. which other people they could be following; suggested followers, ordered by the number of existing common connections.
In this example:
+--------+--------------+----------+
| A | is following | B |
+--------+--------------+----------+
| Arnold | -> | James |
| Arnold | -> | Harrison |
| James | -> | Jennifer |
| James | -> | Harrison |
+--------+--------------+----------+
Between Arnold and James, who can Arnold follow? (excluding existing connections)
The answer should be: Jennifer
This is poor attempt:
db.users.aggregate([
{
$match: { _id: 1 } // Arnold
},
{
$graphLookup: {
from: "follows",
startWith: "$_id",
connectFromField: "following",
connectToField: "follower",
maxDepth: 1,
as: "connections",
}
}
]);
Which results in:
{
"_id": 1,
"name": "Arnold Schwarzenegger",
"connections": [
{
"_id": 24,
"follower": 2,
"following": 4
},
{
"_id": 13,
"follower": 1,
"following": 3
},
{
"_id": 23,
"follower": 2,
"following": 3
},
{
"_id": 12,
"follower": 1,
"following": 2
}
]
}
I believe I need to do some $unwind'ing, but I'm kind of stuck now