PHP json_encoded数组失败mongoimport

时间:2015-11-07 22:38:35

标签: php json mongodb

我有一个PHP脚本,使用json_decode()

转储人员列表
foreach ( $personArray as $person ) {               
    $currentPosition = $person->loadCurrentPosition();
    $one[ FIELD_NAME ] = $person->getName();
    $one[ FIELD_ID ] = $person->getId();
    $one[ FIELD_ORGANIZATION_ID ] = $currentPosition->organization->getId();
    $one[ FIELD_ORGANIZATION_NAME ] = $currentPosition->organization->getName();
    $result[] = $one;
}
$jsonResult = json_encode( $result  );

我将结果放入名为'people.json'的文件中:

[
    {
        "name": "Bobby Brown",
        "id": 32632,
        "organizationid": 40492,
        "organizationname": "Exinda Networks"
    },
    {
        "name": "Billy Bob",
        "id": 32633,
        "organizationid": 29824,
        "organizationname": "Desire2Learn"
    }
]

我使用了mongoimport(mongodb v3.0.7):

mongoimport --db test --collection people --drop --file people.json

我收到以下错误并导入了0个文档:

2015-11-07T17:02:35.461-0500    Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?

我删除文件中的前导方括号'['和尾随']'并再次尝试mongoimport:

2015-11-07T17:13:37.575-0500    Failed: error processing document #2: invalid character ',' looking for beginning of value

我删除了文件中两个JSON对象之间的','并且导入有效:

2015-11-07T17:16:12.162-0500    imported 2 documents

json_encode()编码错误吗?

其中一个json_encode()选项是否会产生mongoimport喜欢的格式?

1 个答案:

答案 0 :(得分:0)

将“--jsonArray”选项添加到mongoimport

非常感谢@ amber4478回答了这个问题Mongoimport of json file