Python Data Access Tutorial
Selected Reading
- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MongoDB - Drop Collection
Python MongoDB - Drop Collection
You can delete collections using drop() method of MongoDB.
Syntax
Following is the syntax of drop() method −
db.COLLECTION_NAME.drop()
Example
Following example drops collection with name sample −
> show collections myColl sample > db.sample.drop() true > show collections myColl
Dropping collection using python
You can drop/delete a collection from the current database by invoking drop() method.
Example
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ example2 ] #Creating a collection col1 = db[ collection ] col1.insert_one({"name": "Ram", "age": "26", "city": "Hyderabad"}) col2 = db[ coll ] col2.insert_one({"name": "Rahim", "age": "27", "city": "Bangalore"}) col3 = db[ myColl ] col3.insert_one({"name": "Robert", "age": "28", "city": "Mumbai"}) col4 = db[ data ] col4.insert_one({"name": "Romeo", "age": "25", "city": "Pune"}) #List of collections print("List of collections:") collections = db.pst_collection_names() for coll in collections: print(coll) #Dropping a collection col1.drop() col4.drop() print("List of collections after dropping two of them: ") #List of collections collections = db.pst_collection_names() for coll in collections: print(coll)
Output
List of collections: coll data collection myColl List of collections after dropping two of them: coll myCollAdvertisements