chapter
Similar to in MongoDB set of SQL database tables.
Create Collection
To create a collection in MongoDB, a database object, passing the name of the collection to be created.
If no collection exists with the same name, MongoDB will create it.
Examples
Create a collection called "customers" are:
import pymongo
myclient = pymongo.MongoClient("mongodb://192.168.31.154:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
IMPORTANT : MongoDB in, not really create a collection before you add content!
MongoDB will wait until after the insertion of a document, really create a collection.
Check whether there is a collection
Remember : MongoDB, the collection is created only when you add content, if this is the first time the collection is created, you should add content (the next chapter, creating a document), and then check whether the collection exists!
The existence of a set of inspection database, we can list all the database collections:
Examples
Returns the database of all collections:
print(mydb.list_collection_names())
Or you can check whether there is a collection:
Examples
Check the "customers" the existence of the collection:
collist = mydb.list_collection_names()
if "customers" in collist:
print("这个集合存在")