I'm uploading data from a JSON file in MongoDB but I want to take each 5 datapoints into 1 field in mongo and I'm using this loop:
document = {}
for key, data_point in zip(fields, data[:5]):
for sub_key, value in data_point.items():
document[f"{key}_{sub_key}"] = value
# Insert Data
collection.insert_one(document)
It works for me on getting 5 datapoints into 1 Mongo field but it only uploads the first 5 and doesn't continue looping to get all 3170 fields that I have which should create 634 fields in Mongo.
you are taking first 5 items. You should iterate through every 5 items. Here example:
chunks = [data[x:x+5] for x in range(0, len(data), 5)]
for chunk in chunks:
document = {}
for i, data_point in enumerate(chunk):
for sub_key, value in data_point.items():
# Create new field key based on the index of the chunk and the sub_key
document[f"{i}_{sub_key}"] = value
# Insert Data
collection.insert_one(document)