! pip install -qU pymongo==4.6.2
! python -m pip install "pymongo[srv]"==3.12
Creating a Database¶
To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database if it does not exist, and make a connection to it.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
Check if DB exits¶
print(myclient.list_database_names())
dblist = myclient.list_database_names()
if "moviesDB" in dblist:
print("The database exists.")
Creating a Collection¶
To create a collection in MongoDB, use database object and specify the name of the collection you want to create. MongoDB will create the collection if it does not exist.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["movies"]
Check if collection exits¶
Or you can check a specific collection by name
collist = mydb.list_collection_names()
if "movies" in collist:
print("The collection exists.")
Insert Into Collection¶
To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method. The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert. Here we would insert 2 Documents with hte insert.many() method.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["movies"]
movielist = [{
"title": "Rocky",
"releaseDate": "1976-12-03",
"genre": "Action",
"about": "A small-time boxer gets a supremely rare chance to fight aheavy-weight champion in a bout in which he strives to go the distance for his self-respect.",
"countries": ["USA"],
"cast": ["Sylvester Stallone", "Talia Shire", "Burt Young"],
"writers": ["Sylvester Stallone"],
"directors": ["John G. Avildsen"]
}, {
"title": "Rambo 4",
"releaseDate": "2008-01-25",
"genre": "Action",
"about": "In Thailand, John Rambo joins a group of mercenaries to venture into war-torn Burma, and rescue a group of Christian aid workers who were kidnapped by the ruthless local infantry unit.",
"countries": ["USA"],
"cast": [" Sylvester Stallone", "Julie Benz", "Matthew Marsden"],
"writers": ["Art Monterastelli", "Sylvester Stallone"],
"directors": ["Sylvester Stallone"]
}]
movies = mycol.insert_many(movielist)
Check if insert method succeeded¶
Ausgabe der MongoDB IDs mit dem Attribut inserted_ids Ausgabe der kompletten Documente mit der find methode in einer einfachen Schleife
print(movies.inserted_ids)
for movies in mycol.find():
print(movies)
Insert another collection¶
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["awards"]
awardslist = [{
"title": "Oscars",
"year": "1976",
"category": "Best Film",
"nominees": ["Rocky","All The President's Men","Bound For Glory","Network","Taxi Driver"],
"winners" :
[
{
"movie" : "Rocky"
}
]
}, {
"title": "Oscars",
"year": "1976",
"category": "Actor In A Leading Role",
"nominees": ["PETER FINCH","ROBERT DE NIRO","GIANCARLO GIANNINI","WILLIAM HOLDEN","SYLVESTER STALLONE"],
"winners" :
[
{
"actor" : "PETER FINCH",
"movie" : "Network"
}
]
}]
awards = mycol.insert_many(awardslist)
Check if insert method succeeded¶
print(awards.inserted_ids)
for awards in mycol.find():
print(awards)
Delete Collections¶
You can delete a table, or collection as it is called in MongoDB, by using the drop() method. Without a collection, there is no database moviesDB too.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["awards"]
mycol.drop()
mycol = mydb["movies"]
mycol.drop()
Delete Database¶
mydb = myclient["moviesDB"]
mydb.drop_database
mydb = myclient["moviesDB"]
mydb.drop_database