通过pymongo在MongoDB中插入数据时出现差异

时间:2016-06-11 11:47:47

标签: mongodb pymongo

我有一个包含20条记录的JSON文件。 我尝试通过python将数据从JSON插入MongoDB(pymongo具体)。 以下是python代码。

    NSDictionary *options = @{
        CBCentralManagerOptionRestoreIdentifierKey : kBlitCentralMgrRestoreIdKey,
        CBCentralManagerOptionShowPowerAlertKey : @YES,
    };
     self.centralMgr = [[[CBCentralManager alloc] initWithDelegate:self queue:nil options:options] autorelease];

len(data)保存值20(通过在python中打印确认)。虽然在运行这个脚本后,没有。插入的记录数为19,未插入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)]) 。 手动将范围更改为(1,21)后,脚本将输入所有20条记录。 我错过了一些非常微不足道的事情。请指出我的错误。

1 个答案:

答案 0 :(得分:0)

对于从0到2的循环,因此运行3次。

for x in range(0, 3):
    print "We're on time %d" % (x)

这意味着您需要从0而非1

进行迭代

reference