MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. It stores data in the form of key-value pairs and is a NoSQL database program. The term NoSQL means non-relational. Refer to MongoDB and Python for an in-depth introduction to the topic. Now let’s understand the use of
rename() function in PyMongo.
The PyMongo function
rename() is used to rename a collection. The rename operation fails if the new name is not an instance of basestring or it is an invalid collection name.
Syntax : rename(new_name, session = None, **kwargs)
- new_name : The new name of the collection.
- session : (Optional) a ClientSession.
- **kwargs : (Optional) additional arguments to the rename command may be passed as keyword arguments to this helper method (i.e. dropTarget = True).
Example 1 : In this example we will create a collection and rename it. The
rename() function will rename the collection name from collection to collec. The value of
dropTarget is set as True, this means that if an existing collection collec existed, then the new collection would overwrite the existing collection’s data.
Example 2 : In this example, the
dropTarget parameter is set to False, the new collection name entered should be unique. But since the collection name collec already exists in the database, it will return an error.
pymongo.errors.OperationFailure: target namespace exists
- Rename multiple files using Python
- Python | Pandas Dataframe.rename()
- Python | Pandas TimedeltaIndex.rename
- Rename all file names in your directory using Python
- Python | Pandas Series.rename()
- Python | os.rename() method
- How to rename columns in Pandas DataFrame
- Rename a folder of images using Tkinter
- Rename specific column(s) in Pandas
- MongoDB and Python
- Guide to Install MongoDB with Python | Windows
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Find
- Python MongoDB - Sort
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Geospatial Queries with Python MongoDB
- Python Mongodb - Delete_one()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.