Mongodb aggregate to find if a user is in any other user's follower list -
i collected followers list , friends list n number of users twitter , stored them in mongodb.
here sample document:
{ "_id": objectid("561d6f8986a0ea57e51ec95c"), "status": "true", "userid": "1489245878", "followers": [ "1566382441", "1155774331" ], "followerscount": 2, "friendscount": 5, "friends": [ "1135511478", "998082481", "565321118", "848123988", "343334562" ] }
i wanted know within collection, there userids in followers list of other documents. lets have user "a", know if user "a" in followers list of other document within same collection. i'm not sure how this. in case if have, project userid , _id of document has userid within followers list.
i guess can use aggregate function below result.
db.getcollection('your_collection").aggregate([ { "$match": { "followers": "1566382441" } }, { "$project": { "followers": 1 } }, { "$unwind": "$followers" }, { "$match": { "followers": "1566382441" } }, { "$group": { "_id": "$followers", "ids": { "$addtoset": "$_id" } } }, { "$project": { "userid": "$_id", "ids": 1, "_id": 0 } } ])
i using sample of data. can add list of users whom trying filter in both stages of "$match"
. see if helps.
p.s: know been long time since asked question! know, never late!
Comments
Post a Comment