-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbConnection.py
41 lines (31 loc) · 1.22 KB
/
MongoDbConnection.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
import json
from os.path import abspath, exists
import mongoengine as me
DB_URI = 'mongodb+srv://helli:Password%21@cluster0-rmyoh.mongodb.net/CardDeck?retryWrites=true&w=majority'
# Create Model object to represnet Deck class
class CardDeck(me.Document):
suit = me.StringField(required=True)
name = me.StringField(required=True)
rank = me.StringField(required=True)
point_value = me.IntField(required=True, max_value=30)
image = me.StringField(required=True)
def __init__(self, suit, name, rank, point_value, image, *args, **kwargs):
super(me.Document, self).__init__(*args, **kwargs)
self.suit = suit
self.name = name
self.rank = rank
self.point_value = point_value
self.image = image
# Connect to Cloud data base with CardDeck
me.connect("CardDeck", host=DB_URI)
# Load Test Json file containing card deck data
json_card_file = abspath("cardeck.json")
if exists(json_card_file):
with open(json_card_file) as deckfile:
deckdata = json.loads(deckfile.read())
for deck in deckdata:
my_deck = CardDeck(**deck)
# Save CardDeck from JSON to DB
my_deck.save()
# Print Saved objects
print(my_deck.to_json(indent=4))