Python MongoDB - Create Collection


A collection in MongoDB holds a set of documents, it is analogous to a table in relational databases.

You can create a collection using the createCollection() method. This method accepts a String value representing the name of the collection to be created and an options (optional) parameter.

Using this you can specify the following −

  • The size of the collection.

  • The max number of documents allowed in the capped collection.

  • Whether the collection we create should be capped collection (fixed size collection).

  • Whether the collection we create should be auto-indexed.


Following is the syntax to create a collection in MongoDB.



Following method creates a collection named ExampleCollection.

> use mydb
switched to db mydb
> db.createCollection("ExampleCollection")
{ "ok" : 1 }

Similarly, following is a query that creates a collection using the options of the createCollection() method.

>db.createCollection("mycol", { capped : true, autoIndexId : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }

Creating a Collection Using Python

Following python example connects to a database in MongoDB (mydb) and, creates a collection in it.


from pymongo import MongoClient

#Creating a pymongo client
client = MongoClient('localhost', 27017)

#Getting the database instance
db = client['mydb']

#Creating a collection
collection = db['example']
print("Collection created........")


Collection created........

Useful Video Courses


Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar


Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty


Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official


Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions


Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It


Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson