我正在使用pymongo插入新集合(Plates_X),当我的导入例程完成后,我想删除旧集合(Plates),然后将新集合的名称更改为旧集合,因此将其切换
重命名后,我确实在Plates中获得了新数据,并获得了我应该命名的集合(Plates),但是旧的临时集合(Plates_X)里面没有任何记录,我无法弄清原因。
def __init__(self, url, db_name, collection_name):
self.connection = pymongo.MongoClient(url)
self.mongo_database = self.connection[db_name]
self.collection = self.mongo_database[collection_name]
switch_over_db(self, plates_table_name):
start_time = datetime.datetime.now()
old_name = self.collection.name
# Drop the old dataset if it exists
if plates_table_name in self.mongo_database.collection_names():
print("dropping collection " + str(plates_table_name))
self.mongo_database.drop_collection(plates_table_name)
# Rename Plates_X to Plates (effectively switching the db)
print("Renaming collection " + str(old_name) + " to " + str(plates_table_name))
self.collection.rename(plates_table_name)
# For some reason have to reselect the collection
self.collection = self.mongo_database[plates_table_name]
# Now build the indexes
self.create_indexes()
end_time = datetime.datetime.now()
print("Switched over db in: " + str(end_time - start_time) + str(" seconds"))
我也不确定为什么在重命名集合并将索引添加到self.collection
之后,为什么实际上将索引添加到了旧的临时集合(Plates_X)。