-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommunities.py
49 lines (42 loc) · 1.1 KB
/
communities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pymongo
from IPython.display import display
from pymongo import MongoClient
import pandas as pd
from dotenv import dotenv_values
config = dotenv_values(".env")
# setup mongo connection
client = MongoClient('mongodb+srv://' + config['MONGO_USER'] + ':' + config['MONGO_PASS'] + '@final-cluster.uucno.mongodb.net')
db = client.final
col = db.StackOverflowPosts
# Aggregation pipeline - count of each tag
pipeline = [
{
"$match": {
"Tags": {"$exists": True, "$type": "string"}
}
},
{
"$addFields": {
"TagsArray": {
"$split": [
{"$substrCP": ["$Tags", 1, {"$subtract": [{"$strLenCP": "$Tags"}, 2]}]},
"><"
]
}
}
},
{"$unwind": "$TagsArray"},
{
"$group": {
"_id": "$TagsArray",
"count": {"$sum": 1}
}
},
{"$sort": {"count": -1}},
{"$limit": 1000}
]
# Execute the aggregation query
docs = list(col.aggregate(pipeline))
# pretty display the resulting docs using pandas
df = pd.DataFrame(docs)
display(df)