Python MongoDB a fassa dake a dake da biki

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"]

Run Instance

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())

Run Instance

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.")

Run Instance