0

I have a directory with 18K json documents. I'm trying to do a bulk import into MongoDB. I wrote this script in Python to load everything in. This code gives me a syntax error since it doesn't seem that Pymongo has 'mongoimport' function. How would I correct this code so that I could do a bulk import of json files from a directory.

import json 
import glob 
from pymongo import MongoClient 

directory = '/home/mongo/data/*.json' 
client = MongoClient("localhost", 27017) 
db = client.nba 
collection = db.test
jsonFiles = glob.glob(directory) 

for file in jsonFiles: 
    mongoimport --db nba --collection sigOptBox --file file
Yolo49
  • 489
  • 2
  • 7
  • 14

1 Answers1

0

mongoimport is a standalone executable. Assuming it is your search path, you can use call from the subprocess module to call it:

import shlex
from subprocess import call
args = shlex.split("mongoimport --db nba --collection sigOptBox --file file")
call(args)
Alex
  • 21,273
  • 10
  • 61
  • 73