Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mongodb - Importing JSON file using mongimport, keep getting `unexpected identifier`?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Answering my own question because complete newbies will appreciate the error and may run into it as they get started.

mongoimport is used from terminal, NOT within the mongo shell. Just like you don't use npm inside node. ;D

After figuring out my conceptual error there, I needed --jsonArray as pointed to by @WiredPrairie in the comments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...