javascript - Meteor: how to sort users by most following? -
welcom ...
i have in meteor project 2 collections
1 - followers
"_id": "_id", "follower": "username1", "following": "username2" }
2- users
"_id": "_id", "username": "username", [...] }
i sort users following how can this
can me ?....
i suggest putting 'followers' collection object inside each 'users' document. there's no point in putting them in separate collections , having reference , forth based on user's id. it's taking more space in db. make object inside each user called 'follows' or similar same structure (though make 'follower' , 'following' arrays). this:
"users":{ "_id":_id, "username":"username", "follows":{ "followers":["username 1","username 2"], "following":["username 3", "username 4"], } }
once have each user document has it's own 'follows' object, can sort users using 'aggregate' functionality mongodb aggregate. following code sorts based on length of followers array. you'd separate 1 'following' swapping out '$followers' "$following".
db.users.aggregate( [ { $project: { "length": { $size: "$followers" } }, { "$sort": { "length": -1 } }, } ] )
this require tweaking, of course. helping lead in right direction.
Comments
Post a Comment