Python MongoDB- rename() Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report rename() method in PyMongo is used to rename a collection within a MongoDB database. It allows you to change the name of an existing collection while keeping its data intact. This is useful for reorganizing or updating collection names without losing data.Syntaxcollection.rename(new_name, dropTarget=False)Parameters:new_name: new name for the collection.dropTarget (optional): Set to True to overwrite an existing collection with the new name. Default is False.Let's look at some examples to understand it better.Example 1: This example creates a collection and renames it from collection to collec. With dropTarget=True, any existing collec will be overwritten. python from pymongo import MongoClient client = MongoClient("mongodb://localhost:27017/") database = client['database'] collection = database['myTable'] docs = [{"id": 1, "name": "Drew"}, {"id": 3, "name": "Cody"}] collection.insert_many(docs) # Rename the collection to 'collec' collection.rename('collec', dropTarget=True) # List and print all collections in the database r = database.list_collection_names(): for name in r: print(name) OutputcollecExample 2 : In this example, dropTarget is set to False, so the new collection name must be unique. Since collec already exists, an error will be raised. python from pymongo import MongoClient client = MongoClient("mongodb://localhost:27017/") db = client['database'] collection = db['myTable'] docs = [{"id": 1, "name": "Drew"}, {"id": 3, "name": "Cody"}] collection.insert_many(docs) # Rename the collection to 'collec' (only if 'collec' does not already exist) collection.rename('collec', dropTarget=False) # List all collections in the database for name in db.list_collection_names(): print(name) Outputpymongo.errors.OperationFailure: target namespace existsRelated Articles:find_one_and_update() queryfind_one_and_delete() querydistinct() querysort() query Comment More info G gauravbabbar25 Follow Improve Article Tags : Python Python-mongoDB Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like