Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming a Collection Using Pymongo

I realize that a collection can be renamed in MongoDB using

db["old_name"].renameCollection("new_name")

But is there an equivalent in PyMongo? I tried the following, but it didn't work either.

db["old_name"].rename_collection("new_name")
like image 655
SKS Avatar asked Feb 21 '18 15:02

SKS


People also ask

How do I change the name of a collection in MongoDB?

In MongoDB, you can use the renameCollection() method to rename or change the name of an existing collection. The new name of the collection. Optional, if true then mongod drops the target of renameCollection former to renaming the collection. The default value is false.

How do I rename a Sharded collection?

There is no way to rename a sharded collection. You can copy all the docs to a new collection. Or create multiple collections based on a weekly/period date, and use as the current one. Then have your application always use the current one by name and change the name at each period break.

How do I change a collection name in MongoDB Atlas?

The renameCollection command renames a collection to the specified new name in the storage configuration. You can run this command only against the admin database, which is the Atlas user authentication database.

How do I update all documents in PyMongo?

Updating all Documents in a Collection. PyMongo includes an update_many() function which updates all the documents which satisfy the given query. filter – It is the first parameter which is a criteria according to which the documents that satisfy the query are updated.


Video Answer


2 Answers

According to the documentation, the method is simply named rename.

rename(new_name, session=None, **kwargs)

     new_name: new name for this 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)
like image 82
rrrr-o Avatar answered Oct 31 '22 17:10

rrrr-o


May be you can use rename in pymongo, just use

db.oldname.rename('newname')

You can check the link: https://api.mongodb.com/python/current/api/pymongo/collection.html?highlight=rename

like image 24
符瑞阳 Avatar answered Oct 31 '22 17:10

符瑞阳