Python MongoDB maak database
- Previous Page MongoDB Introduction
- Next Page MongoDB Create Collection
Maak database
Om een database te maken in MongoDB, moet je eerst een MongoClient-object maken en de verbinding URL specificeren met de juiste IP-adres en de naam van de te maken database.
Als de database niet bestaat, zal MongoDB de database maken en een verbinding opzetten.
Example
Maak een database genaamd "mydatabase":
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"]
Important Note:Databases in MongoDB are not created before content is retrieved!
Before actually creating a database (and collection), MongoDB will keep waiting for you to create at least one collection (table) with a document (record).
Check if the database exists
Remember: In MongoDB, databases are not created before content is retrieved, so if this is your first time creating a database, you should complete the next two chapters (Create Collection and Create Document) before checking if the database exists!
You can check if a database exists by listing all databases in the system:
Example
Return the list of databases in the system:
print(myclient.list_database_names())
Or you can check a specific database by name:
Example
Check if "mydatabase" exists:
dblist = myclient.list_database_names() if "mydatabase" in dblist: print("The database exists.")
- Previous Page MongoDB Introduction
- Next Page MongoDB Create Collection