使用python脚本切换收藏集

时间:2018-07-02 19:52:33

标签: python database mongodb shell collections

我当前正在编写数据迁移脚本,并在脚本中的函数内调用以下行:

output_file.write(process_input_row(row, custom_field_map, args.entry_type, args.entry_prefix, args.book_name))

在对process_input_row的调用中,我正在将数据插入默认情况下定位的当前集合中。我想做的就是调用标准的“ use Collection_Name”,通常会从python脚本的mongo shell中调用它,以便将数据插入正确的集合中。

最简单的方法是什么?我可以在文件顶部添加“ from subprocess import call”,然后执行“ call('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')

提前谢谢!

0 个答案:

没有答案