python - 使用 python 脚本切换集合
问题描述
我目前正在编写一个数据迁移脚本,并在脚本的函数中调用以下行:
output_file.write(process_input_row(row, custom_field_map, args.entry_type, args.entry_prefix, args.book_name))
在对 process_input_row 的调用中,我将数据插入到它默认定位的当前集合中。我想要做的是调用标准的“使用 Collection_Name”,您通常会从 python 脚本中的 mongo shell 调用它,以便将数据插入到正确的集合中。
这样做最简单的方法是什么?我可以将“从子流程导入调用”添加到我的文件顶部并执行类似“调用('use Collection_Name')”之类的操作吗?见下文:
with open(args.entries_file, newline='') as csv_input_file, open(args.output_file, 'w') as output_file:
reader = csv.reader(csv_input_file, delimiter=',', quotechar='"')
row_counter = 0
max_id = 0
for row in reader:
if int(row[1]) > max_id:
max_id = int(row[0])
output_file.write(process_input_row(row, custom_field_map, args.entry_type, args.entry_prefix, args.book_name))
row_counter = row_counter + 1
max_id += 1
# Switch to different collection here
call('use Collection_Name')
提前致谢!
解决方案
推荐阅读
- javascript - 如何使以下正则表达式匹配并替换未闭合的双引号?
- azure - 正在使用设备消息,但未转发到 Azure IOT Hub 中的内置端点
- laravel-backpack - 如何在背包 laravel 中解密 AES256
- python - 时间平均msd的优化
- python - wandb:获取所有工件集合和这些工件的所有别名的列表
- javascript - 如何删除谷歌浏览器中的自动填充背景?
- python-3.x - 通过 API 和 ID 列表从 PubMed 获取完整的论文内容
- azure - 如何在 Kubernetes 中针对 oauth2_proxy 对多个 Azure 应用进行身份验证
- mysql - innodb缓冲池提前预留内存?
- c# - 在 Array1 中查找部分或整个 Array2 的索引位置(顺序)