Instantly share code, notes, and snippets.
Last active
March 25, 2016 23:52
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save breinero-zz/9e5490238be0590c59ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: from the mongo shell | |
// run the following commands | |
// | |
// load( "<path>/SocialGraph.js") | |
// buildGraph() | |
// recommendToFollow().pretty() | |
var lookupFollows = { | |
$lookup: { | |
from: 'follows', | |
localField: '_id.to', | |
foreignField: '_id.from', | |
as: 'following' | |
} | |
} | |
var project = { | |
$project: { | |
recomendee: "$_id.from", | |
follower: "$_id.to", | |
recommended: "$following._id.to", | |
_id: 0 | |
} | |
} | |
var unwindRecommended = { $unwind: "$recommended" } | |
var groupEdge = { | |
$group: { | |
_id: { recommendee: "$recomendee", recommended: "$recommended" }, | |
count: { $sum: 1 }, | |
followedBy: { $addToSet: "$follower" } | |
} | |
} | |
var groupByRecomendee = { | |
$group: | |
{ | |
_id: "$_id.recommendee", | |
recommendations :{ | |
$push: { | |
count: "$count", | |
recommendation: "$_id.recommended", | |
followedBy: "$followedBy" | |
} | |
} | |
} | |
} | |
var unwindRecommendations = | |
{ $unwind: "$recommendations" } | |
var sortByCount = | |
{ $sort: { "recommendations.count": -1 } } | |
var groupByRecomendeeAgain = { | |
$group: { | |
_id: "$_id", | |
recommendations: { | |
$push: { | |
recommendation: "$recommendations.recommendation", | |
count: "$recommendations.count", | |
followers: "$recommendations.followedBy" | |
} | |
} | |
} | |
} | |
var truncate = { | |
$project: { | |
name: "$_id", | |
recommendations: { $slice: [ "$recommendations", 5 ] } | |
} | |
} | |
// find your follower's followers | |
function recommendToFollow() { | |
return db.follows.aggregate( | |
lookupFollows, | |
project, | |
unwindRecommended, | |
groupEdge, | |
groupByRecomendee, | |
unwindRecommendations, | |
sortByCount, | |
groupByRecomendeeAgain, | |
truncate | |
); | |
} | |
var names = [ | |
"Maryln Bade", | |
"Loria Yon", | |
"Daisey Delzell", | |
"Milo Schaible", | |
"Lanette Slayton", | |
"Matha Loper", | |
"Rosalva Amador", | |
"Lesley Borum", | |
"Tonja Blowers", | |
"Lucile Billingslea", | |
"Sebastian Cumbee", | |
"Cythia Stein", | |
"Roland Belisle", | |
"Lorri Millington", | |
"Hildred Catt", | |
"Kaitlyn Folkerts", | |
"Tory Sim", | |
"Elene Shipp", | |
"Charolette Godines", | |
"Elizebeth Giberson", | |
"Elli Kenney", | |
"Mignon Koogler", | |
"Herman Breuer", | |
"Ahmad Brick", | |
"Catherine Coward", | |
"Andreas Osterberg", | |
"Elyse Sumner", | |
"Adelaide Donner", | |
"Mozelle Hynson", | |
"Latia Mcguigan", | |
"Carola Jared", | |
"Van Quijada", | |
"Theda Tootle", | |
"Ursula Luedke", | |
"Eleonora Walberg", | |
"Carson Clukey", | |
"Penni Loop", | |
"Tommye Angevine", | |
"Delia Elkins", | |
"Obdulia Hayner", | |
"Roseann Bautch", | |
"Lindsay Winder", | |
"Verda Overturf", | |
"Luke Tingey", | |
"Harvey Sikora", | |
"Doug Lincoln", | |
"Elinore Hoadley", | |
"Julene Blansett", | |
"Riva Suther", | |
"Erlene Manis" | |
] | |
// create a social graph, initialize the examples | |
function buildGraph() { | |
for( var i in names ) { db.names.insert( { _id: names[i] }); } | |
db.names.find().forEach( | |
function( from ) { | |
var numFriends = Math.floor((Math.random() * 20) + 1); | |
db.names.aggregate ( | |
{ $sample: { size: numFriends } } | |
).forEach( | |
function( to ) { | |
if( from._id === to._id ) return; | |
db.follows.insert( { _id: { from: from._id, to: to._id } } ); | |
} | |
); | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment