Python MongoDB Create Collection


The MongoDB collection is the same as the SQL database table.


Creating a Collection

Use the database object and indicate the name of the collection that you wish to construct to create a MongoDB collection.

If the collection doesn't exist, automatically MongoDB creates the collection.

Example :- Create a collection called "emp" :

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['mydatabase']

mycol = mydb["emp"]

print("Collection created!")

Output :-

Collection created!

Important: A collection is not generated in MongoDB until it gets a document!

MongoDB waits until you insert a document before the collection is truly created.

You can also search for these topics, python mongodb create collection dynamically, how to use mangodb collection to create using python, key values for python mangodb create collection, list the mangodb create collection in python, python multiple options to create a collections for mangodb, give the permissions to create a collection with python mangodb, primary key used for python mangodb create collection, how to remove the generated collections in mangodb using python.

Check if Collection Exists

You should create a document before you check that a collection exists.

If this is your first time creating a collection then the name of collection will not be displayed until it get atleast a document in it.

The list_collection_names() method is used to return all available collection names from mongodb, which contains atleast one document.

Example :- Return all collection names from "mydatabase" :

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['mydatabase']

mycol = mydb["emp"]

print(mydb.list_collection_names())

Output :-

['emp']

Note:- In our case, We have added few documents on it.

You can also search for these topics, python check if collection exists file, check the collections into the folder using python mangodb, existing to check the collection exists in python mangodb database, check if collection exists create function using python mangodb, group the collection and check the existing with python mangodb, property of collection in python mangodb to check existing.

Delete Collection

By utilising the drop() method, you can remove a table or collection as it is called in MongoDB.

Example :- Delete the "emp" collection :

import pymongo

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

mycol.drop()

print("Collection deleted")

Output :-

Collection deleted

If the collection has been dropped successful the drop() method returns true and false when the collection doesn't exist.

You can also search for these topics, python mongodb drop collection all documents, python to duplicate the droped collection with mangodb, example for python mangodb delete the collection, empty the python mangodb to delete collections, delete collection free space using python mangodb, large and multiple collection of data using python, python mangodb working of drop collection and rename it, Example for python mangodb drop collection.