bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Python/Databases in Python
Python•Databases in Python

Python MongoDB Create Collection

A collection in MongoDB is the same as a table in SQL databases.

Creating a Collection

To create a collection in MongoDB, use database object and specify the name of the collection you want to create.

MongoDB will create the collection if it does not exist.

Example

Create a collection called "customers":

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

Important: In MongoDB, a collection is not created until it gets content!

MongoDB waits until you have inserted a document before it actually creates the collection.

Check if Collection Exists

Remember

In MongoDB, a collection is not created until it gets content, so if this is your first time creating a collection, you should complete the next chapter (create document) before you check if the collection exists!

You can check if a collection exist in a database by listing all collections:

Example

Return a list of all collections in your database:

print(mydb.list_collection_names())

Or you can check a specific collection by name:

Example

Check if the "customers" collection exists:

collist = mydb.list_collection_names()
if "customers" in collist:
 print("The collection exists.")

Previous

Python MySQL Create Table

Next

Python MySQL Insert Into Table

This chapter

Overview
23

Lessons

49m

Read time

1. Python MySQL2. Python MongoDB3. Python MySQL Create Database4. Python MongoDB Create Database5. Python MySQL Create Table6. Python MongoDB Create Collection7. Python MySQL Insert Into Table8. Python MongoDB Insert Document9. Python MySQL Select From10. Python MongoDB Find11. Python MySQL Where12. Python MongoDB Query13. Python MySQL Order By14. Python MongoDB Sort15. Python MySQL Delete From By16. Python MongoDB Delete Document17. Python MySQL Drop Table18. Python MongoDB Drop Collection19. Python MySQL Update Table20. Python MongoDB Update21. Python MySQL Limit22. Python MongoDB Limit23. Python MySQL Join

On this page

Creating a CollectionCheck if Collection Exists