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")
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.
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.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With