Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/import_overwrite_generic.py
1128 views
1
# File importing a json file to the mongodb database
2
# Usage: python import_generic.py filename.json database_name.collection_name
3
# A safeguard is currently inserted, making sure database_name == "limbo"
4
# because there is a risk of overwriting an already existing collection
5
6
import sys
7
8
def main(argv):
9
file_name = argv[0]
10
database_collection = argv[1]
11
assert "." in database_collection
12
database_name, collection_name = database_collection.split(".")
13
# If you know what you are doing, remove the line below.
14
# In general you should just avoid using this with an existing collection as it might overwrite information
15
assert database_name == "limbo"
16
17
print("Importing file ", file_name, " to database ", database_name, " and collection ", collection_name)
18
import json
19
import sys
20
sys.path.append("../")
21
import base
22
print("getting connection")
23
base._init(37010, "")
24
print("I have it")
25
26
C = base.getDBConnection()
27
28
collection = C[database_name][collection_name]
29
print("Got a collection: ", collection)
30
with open(file_name, "r") as f:
31
print("Loading data in memory")
32
data = json.load(f)
33
print("Done")
34
print("There are ", len(data), " items ")
35
import sys
36
sys.path.append("../")
37
print("Uploading")
38
for x in data:
39
try:
40
collection.save(x)
41
except OverflowError:
42
print(x)
43
raise OverflowError
44
print("Done")
45
46
if __name__ == "__main__":
47
main(sys.argv[1:])
48
49