-
Notifications
You must be signed in to change notification settings - Fork 1
/
insert.py
35 lines (31 loc) · 1.07 KB
/
insert.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
import csv
from pymongo import MongoClient
wantToReset = True
client = MongoClient('mongodb://localhost:27017/')
db = client['database']
collection = db['coffee_shops']
if wantToReset:
collection.delete_many({})
print("Collection cleared to insert more")
with open('data.csv', 'r') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
latitude = float(row['lat'])
longitude = float(row['long'])
collection.insert_one({
'name': row['name'],
'location': {
'address': row['address'],
'coordinates': [latitude, longitude]
},
'averageRating': float(row['averageRating']),
'numRatings': int(row['numRatings']),
'cost': int(row['cost']),
'bathrooms': row['bathrooms'],
'wifi':row['wifi'],
'noise':row['noise'],
'studyability':row['studyability'],
'ratings': row['ratings'],
'imgurl': row['imgurl'],
})
print("Data imported successfully into MongoDB.")