-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
32 lines (24 loc) · 998 Bytes
/
db.py
File metadata and controls
32 lines (24 loc) · 998 Bytes
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
# Read the Excel sheet
df = pd.read_excel('user_log.xlsx')
# Group data by IP address
grouped_data = df.groupby('ipAddress')
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['user_behaviour']
collection = db['userdata']
# Iterate over groups and store data in MongoDB
for ipAddress, group in grouped_data:
# Convert group data to a dictionary or JSON format
data_to_insert = group.to_dict(orient='records')
# Check if the user (IP address) already exists in the collection
existing_user = collection.find_one({'ipAddress': ipAddress})
if existing_user:
# If user exists, append data to the existing document
collection.update_one(
{'ip_address': ipAddress},
{'$push': {'user_behavior': {'$each': data_to_insert}}}
)
else:
# If user does not exist, insert a new document
collection.insert_one({'ip_address': ipAddress, 'user_behavior': data_to_insert})
client.close()