Path: blob/main/scripts/import_overwrite_generic.py
1128 views
# File importing a json file to the mongodb database1# Usage: python import_generic.py filename.json database_name.collection_name2# A safeguard is currently inserted, making sure database_name == "limbo"3# because there is a risk of overwriting an already existing collection45import sys67def main(argv):8file_name = argv[0]9database_collection = argv[1]10assert "." in database_collection11database_name, collection_name = database_collection.split(".")12# If you know what you are doing, remove the line below.13# In general you should just avoid using this with an existing collection as it might overwrite information14assert database_name == "limbo"1516print("Importing file ", file_name, " to database ", database_name, " and collection ", collection_name)17import json18import sys19sys.path.append("../")20import base21print("getting connection")22base._init(37010, "")23print("I have it")2425C = base.getDBConnection()2627collection = C[database_name][collection_name]28print("Got a collection: ", collection)29with open(file_name, "r") as f:30print("Loading data in memory")31data = json.load(f)32print("Done")33print("There are ", len(data), " items ")34import sys35sys.path.append("../")36print("Uploading")37for x in data:38try:39collection.save(x)40except OverflowError:41print(x)42raise OverflowError43print("Done")4445if __name__ == "__main__":46main(sys.argv[1:])474849