0
我有一個包含20條記錄的JSON文件。 我試圖通過python將數據從JSON插入到MongoDB中(pymongo是具體的)。 以下是python代碼。通過pymongo在MongoDB中插入數據時的差異
from pymongo import MongoClient
from pprint import pprint
import json
# creating a mongo db client and connecting with
# the fantasyscout database.
# Inserting the players in the collection(table)
# players.
client = MongoClient()
db = client.fantasyscout
# Reading the data from the dumper json file
with open("C:\\team.level.json") as db_data:
data = json.load(db_data)
# Reading every element in the JSON data and
# inserting in the db.
print len(data)
print data[str(20)]
for element in range(1,++len(data)):
db.team_level.insert(data[str(element)])
len(data)保存值20(通過python打印確認)。雖然運行這個腳本後,沒有。插入的記錄是19,並且未插入data["20"]
。 將範圍手動更改爲(1,21)後,腳本將輸入全部20條記錄。 我錯過了一些非常微不足道的東西。請指出我的錯誤。