使用pymongo 批量重命名 mongodb的collection

应用背景:

工作中有一项内容就是重命名collection的, 涉及到几十个db, 一个一个去改, 也OK,但我觉得那是个体力活, 不应该是程序员解决问题的方法

解决过程:

通过查询,发现网上很多宝宝推荐使用 renameCollection() 方法来解决问题, 这个方法,在Robo 3T里可以使用, 但达不到我想要的结果, 我需要的是批量, 把这个方法放到python里, 报错:

TypeError: ‘Collection’ object is not callable. If you meant to call the ‘renameCollection’ method on a ‘Collection’ object it is failing because no such method exists.

度娘了N多网站, 发现都是在推荐renamecollection()方法, 怎么办??

那个谁说过, 工作现场有神灵, 去看看pymongo文档吧,
在文档里发现,rename()方法,完美解决问题.

使用方法如下:

mongourl = 'mongodb://user:password@host:port/admin?replicaSet=mgset-1100081&socketTimeoutMS=10000'
dbstr = 'test'
client = pymongo.MongoClient(mongourl)
db = client[dbstr]
db.oldname.rename('newname')

–the end–

猜你喜欢

转载自blog.csdn.net/weixin_39791387/article/details/81562797
今日推荐