Jupyter Notebook
Einbau Rating¶
Erweitern Sie das imdb-Rating iin der Datenbank moviesDB mit den Anzahl votes (Stimmen). Erweitern Sie das JSON-Dokument mit dem Tomatometer (tomatoes), welches - Benutzer (viewer) mit Rating (Decimal) und Anzahl Stimmen (Integer) beinhaltet - Kritiker (critics) mit Rating (Decimal) und Anzahl Stimmen (Integer) beinhaltet - Ein "frische"-Rating (fresh) beinhaltet (ganze Zahl) - Ein "verrottet"-Rating (rotten) beinhaltet (ganze Zahl)
In [15]:
Copied!
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["movies"]
movielist = [{
"id": 14253,
"title": "Beauty and the Beast",
"year": 2016,
"language": "English",
"imdb_rating": {
"rating": 7.1,
"votes" : 341312
},
"tomatoes": {
"viewer": {
"rating": 3.9,
"votes": 238
},
"criticer": {
"rating": 4.2,
"votes": 238
},
"fresh": 9,
"rotten": 2
},
"genre": "Romance",
"director": "Christophe Gans",
"runtime": 112
}]
movies = mycol.insert_many(movielist)
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["movies"]
movielist = [{
"id": 14253,
"title": "Beauty and the Beast",
"year": 2016,
"language": "English",
"imdb_rating": {
"rating": 7.1,
"votes" : 341312
},
"tomatoes": {
"viewer": {
"rating": 3.9,
"votes": 238
},
"criticer": {
"rating": 4.2,
"votes": 238
},
"fresh": 9,
"rotten": 2
},
"genre": "Romance",
"director": "Christophe Gans",
"runtime": 112
}]
movies = mycol.insert_many(movielist)
Check if insert method succeeded¶
Ausgabe der kompletten Documente mit der find methode in einer einfachen Schleife
In [16]:
Copied!
print(movies.inserted_ids)
print(movies.inserted_ids)
[ObjectId('67581c3fedd26fb5227e4233')]
Check if DB exits¶
In [17]:
Copied!
# Liste aller existierenden Datenbanken abrufen
dblist = myclient.list_database_names()
# Überprüfen, ob die Datenbank existiert
if "moviesDB" in dblist:
print("Datenbank existiert.")
else:
print("Datenbank existiert nicht.")
# Liste aller existierenden Datenbanken abrufen
dblist = myclient.list_database_names()
# Überprüfen, ob die Datenbank existiert
if "moviesDB" in dblist:
print("Datenbank existiert.")
else:
print("Datenbank existiert nicht.")
Datenbank existiert.
Check if DB exits¶
In [18]:
Copied!
# Liste aller Sammlungen in der Datenbank abrufen
collist = mydb.list_collection_names()
# Überprüfen, ob die Sammlung existiert
if "movies" in collist:
print("Sammlung existiert.")
else:
print("Sammlung existiert nicht.")
# Liste aller Sammlungen in der Datenbank abrufen
collist = mydb.list_collection_names()
# Überprüfen, ob die Sammlung existiert
if "movies" in collist:
print("Sammlung existiert.")
else:
print("Sammlung existiert nicht.")
Sammlung existiert.
Create a Collection¶
In [19]:
Copied!
# Erstellung der Sammlung "good"
mydb.create_collection("good")# python code
# Erstellung der Sammlung "good"
mydb.create_collection("good")# python code
Out[19]:
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'moviesDB'), 'good')
Check if collection exits¶
In [20]:
Copied!
# Check if the 'good' collection exists in the database
if "good" in mydb.list_collection_names():
print("Collection 'good' exists.")
else:
print("Collection 'good' does not exist.")
# Check if the 'good' collection exists in the database
if "good" in mydb.list_collection_names():
print("Collection 'good' exists.")
else:
print("Collection 'good' does not exist.")
Collection 'good' exists.
Insert Into Collection¶
In [21]:
Copied!
# Inserting a single document into the 'good' collection
movie = {
"title": "Inception",
"year": 2010,
"genre": "Science Fiction",
"director": "Christopher Nolan",
"imdb_rating": {
"rating": 8.8,
"votes": 1600000
}
}
# Insert the document
result = mycol.insert_one(movie)
# Inserting a single document into the 'good' collection
movie = {
"title": "Inception",
"year": 2010,
"genre": "Science Fiction",
"director": "Christopher Nolan",
"imdb_rating": {
"rating": 8.8,
"votes": 1600000
}
}
# Insert the document
result = mycol.insert_one(movie)
Check if insert method succeeded¶
In [22]:
Copied!
print(movies.inserted_ids)
for movies in mycol.find():
print(movies)
print(movies.inserted_ids)
for movies in mycol.find():
print(movies)
[ObjectId('67581c3fedd26fb5227e4233')]
{'_id': ObjectId('67581c36edd26fb5227e422f'), 'id': 14253, 'title': 'Beauty and the Beast', 'year': 2016, 'language': 'English', 'imdb_rating': {'rating': 7.1, 'votes': 341312}, 'tomatoes': {'viewer': {'rating': 3.9, 'votes': 238}, 'criticer': {'rating': 4.2, 'votes': 238}, 'fresh': 9, 'rotten': 2}, 'genre': 'Romance', 'director': 'Christophe Gans', 'runtime': 112}
{'_id': ObjectId('67581c3eedd26fb5227e4231'), 'id': 14253, 'title': 'Beauty and the Beast', 'year': 2016, 'language': 'English', 'imdb_rating': {'rating': 7.1, 'votes': 341312}, 'tomatoes': {'viewer': {'rating': 3.9, 'votes': 238}, 'criticer': {'rating': 4.2, 'votes': 238}, 'fresh': 9, 'rotten': 2}, 'genre': 'Romance', 'director': 'Christophe Gans', 'runtime': 112}
{'_id': ObjectId('67581c3fedd26fb5227e4233'), 'id': 14253, 'title': 'Beauty and the Beast', 'year': 2016, 'language': 'English', 'imdb_rating': {'rating': 7.1, 'votes': 341312}, 'tomatoes': {'viewer': {'rating': 3.9, 'votes': 238}, 'criticer': {'rating': 4.2, 'votes': 238}, 'fresh': 9, 'rotten': 2}, 'genre': 'Romance', 'director': 'Christophe Gans', 'runtime': 112}
{'_id': ObjectId('67581c4cedd26fb5227e4234'), 'title': 'Inception', 'year': 2010, 'genre': 'Science Fiction', 'director': 'Christopher Nolan', 'imdb_rating': {'rating': 8.8, 'votes': 1600000}}
Insert another collection¶
In [23]:
Copied!
mydb.create_collection("collection")
mydb.create_collection("collection")
Out[23]:
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'moviesDB'), 'collection')
Check if insert method suceeded¶
In [24]:
Copied!
if "collection" in mydb.list_collection_names():
print("Collection exists.")
else:
print("Collection does not exist.")
if "collection" in mydb.list_collection_names():
print("Collection exists.")
else:
print("Collection does not exist.")
Collection exists.
Delete Collections¶
In [25]:
Copied!
mycol.drop()
mycol.drop()
In [ ]:
Copied!
## Um gezielte Kollektion zu löschen:
mycol = mydb["collection"]
mycol.drop()
## Um gezielte Kollektion zu löschen:
mycol = mydb["collection"]
mycol.drop()
Delete Database¶
In [26]:
Copied!
myclient.drop_database("moviesDB")
myclient.drop_database("moviesDB")