[英]How to create a database in Mongo using Python?
我編寫了下面的 Python 程序來創建 MongoDB。
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['mydatabase']
dblist = myclient.list_database_names()
if "mydatabase" in dblist:
print("The database exists.")
else :
print("The database does not exists.")
但是當我執行程序時,我得到的結果是
數據庫不存在。
為什么這樣? 有什么我想念的嗎?
我正在遵循W3Schools 教程中提到的代碼
在 MongoDB 中,在您將一些數據放入其中之前,該數據庫並未真正創建,請嘗試將文檔放入其中並再次檢查。
重要提示:在 MongoDB 中,在獲取內容之前不會創建數據庫!
數據庫是延遲創建的。 將某些內容插入該數據庫的集合中,它們(數據庫/集合)將 spring 存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.