![]() |
VOOZH | about |
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.
collection.rename(new_name, dropTarget=False)
Parameters:
Let's look at some examples to understand it better.
This example creates a collection and renames it from collection to collec. With dropTarget=True, any existing collec will be overwritten.
Output
collec
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.
Output
pymongo.errors.OperationFailure: target namespace exists
Related Articles: