-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
112 lines (94 loc) · 3.12 KB
/
example.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from pprint import pprint
from DbConnector import DbConnector
class ExampleProgram:
def __init__(self):
self.connection = DbConnector()
self.client = self.connection.client
self.db = self.connection.db
def create_coll(self, collection_name):
collection = self.db.create_collection(collection_name)
print('Created collection: ', collection)
def insert_documents(self, collection_name):
# Dummy user data
users = [
{
"user_id": 1,
"has_label": True,
"activities": [12345, 23456, 34567]
},
{
"user_id": 2,
"has_label": False,
"activities": [67890, 78901]
}
# Add more user data as needed
]
# Dummy activity data
activities = [
{
"transport_mode": "walk",
"start_date_time": "2021-10-15 10:00:00",
"end_date_time": "2021-10-15 12:00:00",
"user_id": 1
},
{
"transport_mode": "car",
"start_date_time": "2021-10-16 15:00:00",
"end_date_time": "2021-10-16 16:30:00",
"user_id": 2
}
# Add more activity data as needed
]
# Dummy trackpoint data
trackpoints = [
{
"lat": 10,
"lon": 11,
"altitude": 123,
"date_time": "2021-10-15 10:30:00",
"user_id": 1,
"activity": 12345
},
{
"lat": 15,
"lon": 18,
"altitude": 100,
"date_time": "2021-10-16 15:15:00",
"user_id": 2,
"activity": 67890
}
# Add more trackpoint data as needed
]
collection = self.db[collection_name]
#collection.insert_many(users)
collection.insert_many(activities)
#collection.insert_many(trackpoints)
def fetch_documents(self, collection_name):
collection = self.db[collection_name]
documents = collection.find({})
for doc in documents:
pprint(doc)
def drop_coll(self, collection_name):
collection = self.db[collection_name]
collection.drop()
def show_coll(self):
collections = self.client['test'].list_collection_names()
print(collections)
def main():
program = None
try:
program = ExampleProgram()
program.create_coll(collection_name="activities")
program.show_coll()
program.insert_documents(collection_name="activities")
program.fetch_documents(collection_name="activities")
#program.drop_coll(collection_name="activities")
# Check that the table is dropped
program.show_coll()
except Exception as e:
print("ERROR: Failed to use database:", e)
finally:
if program:
program.connection.close_connection()
if __name__ == '__main__':
main()