Python MongoDB a fassa dake a dake da biki
- Baya Previous MongoDB Anfani Dabici
- Baya Next MongoDB Dafin Dauai Tsarin Tabirai
Dake da biki a MongoDB da yau dake da tabi a SQL dake da biki.
A fassa dake a dake da biki
Iya a fassa dake a MongoDB, za a fassa dake a dake da biki.
Baiwa baiwa, MongoDB za a fassa dake.
Instance
A fassa dake da biki "customers"
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"]
Important Tip:In MongoDB, collections are not created before obtaining content!
Before actually creating a collection, MongoDB will wait until you have inserted a document.
Check if the collection exists
Remember: In MongoDB, collections are not created before obtaining content, so if this is your first time creating a collection, you should complete the next chapter (create document) before checking if the collection exists!
You can check if a collection exists in the database by listing all collections:
Instance
Return a list of all collections in the database:
print(mydb.list_collection_names())
Or you can check a specific collection by name:
Instance
Check if the "customers" collection exists:
collist = mydb.list_collection_names() if "customers" in collist: print("The collection exists.")
- Baya Previous MongoDB Anfani Dabici
- Baya Next MongoDB Dafin Dauai Tsarin Tabirai