使用Python刪除MongoDB中已存在的集合
MongoDB是一個廣受歡迎的開源資料庫,它以靈活的類似JSON的格式儲存資料。它不使用傳統的行列式儲存資料技術,而是採用更靈活的方法,從而提高了可擴充套件性。
該資料庫旨在處理海量資料,因此非常適合現代應用程式。MongoDB資料庫由“集合”組成,這類似於RDBMS中的表。
集合是由包含不同型別值的欄位組成的文件的集合。一個數據庫可以包含多個集合,每個集合可以包含多個文件。在本文中,我們將使用Python命令刪除MongoDB集合。每個集合都有自己的模式,這取決於文件的結構。
安裝PyMongo
PyMongo是程式設計師與“MongoDB”資料庫互動的Python驅動程式。它提供了一個介面,可以使用Python對MongoDB資料執行多種操作。我們可以使用命令列上的Python包管理器安裝“PyMongo”:
pip install pymongo
安裝PyMongo庫後,我們可以在本地IDE中匯入它。
建立資料庫
我們需要一個我們將對其進行操作的參考資料庫。建立MongoDB資料庫並非難事。我們必須從網際網路下載最新版本的MongoDB並將其安裝在系統上。之後,我們將啟動“MongoDB”伺服器。我們可以使用預設伺服器和預設埠號,並開始“連線”過程。我們可以透過傳遞資料庫名稱和集合名稱來手動建立資料庫。資料可以以JSON或CSV檔案格式匯入。
透過PyMongo將MongoDB連線到Python
這是最重要的一步,因為它涉及到兩個平臺之間連線的建立。我們將使用“pymongo.MongoClient()”函式建立一個MongoClient物件。透過將伺服器地址作為此函式的引數來建立連線。
語法
Mongo_client = pymongo.MongoClient("Connection address")
讓我們應用此方法來建立連線。
在Python中建立連線以讀取集合
這裡我們嘗試讀取儲存在MongoDB中的集合。在下面的例子中:
我們匯入了“PyMongo”庫並建立了一個“MongoClient”物件,它允許我們建立連線並訪問資料庫。
我們傳遞了一個伺服器地址,將地址名稱指定為“localhost”,這意味著MongoDB伺服器與Python程式執行在同一臺機器上。我們使用了MongoDB伺服器的預設埠號:“27017”。
之後,我們指定了資料庫和集合名稱。
我們建立了一個集合並填充了它。
我們使用“find()”方法檢索儲存在集合中的文件。
示例
import pymongo Mongo_client = pymongo.MongoClient("mongodb://:27017/") # Database name database = Mongo_client["mydb"] #Getting the database instance database = Mongo_client['mydb'] #Creating a collection collection = database['example'] #Inserting document into the collection data = [{"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}] res = collection.insert_many(data) print("Data inserted ......") #Retreving the data documents = collection.find() print("Contents of the collection: ") for document in documents: print(document)
輸出
Data inserted ...... Contents of the collection: {'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'} {'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'} {'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
現在,我們已經建立了一個數據庫和一個集合,讓我們來看看從資料庫中刪除集合的方法。
使用drop()方法刪除集合
這是一個非常簡單的從資料庫中刪除集合的方法。讓我們來了解一下。
建立連線後,我們使用drop()方法從資料庫中刪除目標集合。
刪除集合後,我們將無法使用“find()”方法檢索其文件。
由於集合已被刪除,因此返回“None”作為輸出。
示例
import pymongo Mongo_client = pymongo.MongoClient("mongodb://:27017/") # Database name database = Mongo_client["mydb"] #Getting the database instance database = Mongo_client['mydb'] #Creating a collection collection = database['example'] documents = collection.find() print("Contents of the collection: ") for document in documents: print(document) #dropping the collection print(collection.drop()) print("Collection Dropped ......")
輸出
F:\Examples>python test.py Contents of the collection: {'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'} {'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'} {'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'} None Collection Dropped ......
如果您嘗試開啟MongoDB資料庫並驗證集合,您會發現該集合
結論
本文重點介紹了使用Python程式設計刪除資料庫中存在的“集合”這一簡單的“MongoDB”操作。我們使用“PyMongo”庫來訪問MongoDB資料庫。我們建立了連線並指定了目標資料庫和集合名稱。最後,我們使用“drop()”方法從資料庫中刪除集合。