29

I'm trying to add a JSON file to mongodb using mongoimports from terminal, here:

 mongoimport --db my_db --collection my_collection --file /content/2_read.json

I keep getting JavaScript execution failed: SyntaxError: Unexpected identifier I ran my JSON through JSON Lint: http://jsonlint.com/ which says it's valid JSON.

I'm not sure what could be tripping up the import process?? Or how to investigate further to hunt down the issue?

UPDATE

  1. Somebody suggested putting all on one-line. A decent suggestion. I tried it and it didn't work.
  2. I tried importing a very very simple json file named simple.json with the content {'content' : 'simple'} But I'm still getting the same error using mongoimport. (I can add documents from mongo shell just fine.)

A sample of the JSON is below.

2_read.json

{
  "name" : "John",
  "tasks" : [
      {
       "ix" : "1",
       "description" : "description of task",
       "tags": []
      }, 
     {
      "ix": "2",
      "description" : "description of task",
      "tags" : []
     }
       ]
}

Thanks.

4

2 回答 2

126

回答我自己的问题,因为完全的新手会欣赏这个错误,并且在他们开始时可能会遇到它。

mongoimport从终端使用,而不是在mongo外壳内。就像你不使用npminside一样node。;D

在找出我的概念错误之后,我需要@WiredPrairie 在评论中指出的 --jsonArray 。

于 2013-05-11T19:14:09.873 回答
0

来自终端的以下命令对我很有用:

mongoimport -d my_db -c my_collection < /content/2_read.json --batchSize 1
于 2015-11-30T08:56:11.877 回答