2015-09-07 90 views
3

我正試圖按照本文中的步驟http://zaiste.net/2012/08/importing_json_into_mongodb/將名爲breedData.json的文件導入到mongodb中。mongoimport未能導入JSON數據

所以我在終端從我的應用的根文件夾中輸入下面的命令。 breedData.json文件也位於應用程序根文件夾中。 Mongod和nodemon正在運行。

mongoimport --db shelterdoggie --collection breeds --type json --file breedData.json --jsonArray 

,我得到這個:

​​

我檢查了我的文件,jsonlint.com,它是有效的JSON。

我使用上述終端命令這個JSON格式的嘗試:

[ 
{"malehw":"Ht: 9-11.5, Wt: 7-9","femalehw":"Ht: 9-11.5, Wt: 7-9"}, 
{"malehw":"Ht: 27, Wt: 60","femalehw":"Ht: 25, Wt: 50"}, 
{"malehw":"Ht: 23, Wt: 45","femalehw":"Ht: <23, Wt: 45"} 
] 

,我也試過這種格式,而上面留下關閉--jsonArray標誌命令:

{"malehw":"Ht: 9-11.5, Wt: 7-9","femalehw":"Ht: 9-11.5, Wt: 7-9"} 
{"malehw":"Ht: 27, Wt: 60","femalehw":"Ht: 25, Wt: 50"} 
{"malehw":"Ht: 23, Wt: 45","femalehw":"Ht: <23, Wt: 45"} 

但後來我得到這個錯誤:

2015-09-07T01:11:00.034-0700 connected to: localhost 
2015-09-07T01:11:00.035-0700 Failed: error processing document #1: invalid character '{' after array element 
2015-09-07T01:11:00.035-0700 imported 0 documents 

我檢查了該文件,並有152 {'S和152},我在文件中有152行/文檔,所以它不應該是由於某個地方沒有封閉的大括號。我不明白爲什麼這不起作用。如果您有任何建議,我會很感激。

下面是情況下,從我以.json文件一個全線它可以幫助:

{"malehw":"Ht: 24-26, Wt: 75-95","femalehw":"Ht: 22-24, Wt: 75-95","catfriendly":"••••","easytraining":"••••••","watchdog":"••••••","grooming":"•••","coldtolerant":"••••","care":"Among the most intelligent of breeds, the German Shepherd Dog is so intent on his mission whatever that may be and he is virtually unsurpassed in working versatility. He is utterly devoted and faithful. He is usually good with other pets.","breed":"German Shepherd Dog","health":"This breed needs daily mental and physical challenges. He enjoys a good exercise session as well as learning session. He is family-oriented and does well as a house dog. His coat needs brushing one or two times weekly.","energy":"••••","playfulness":"•••","dogfriendly":"••","strangerfriendly":"•••","protection":"••••••","heattolerant":"••••","exercise":"•••••","affection":"••••","index":67,"url":"https://www.petfinder.com/dog-breeds/German-Shepherd-Dog"} 
+0

我能夠運行所有的疑問沒有任何錯誤,甚至我創建了一個完整的最後一行數組(剛剛複製同一行),並且它仍然在我的shell中正常工作。檢查你是否指的是正確的文件和路徑。 –

+0

另外,我正在使用MongoDB shell版本:3.0.4。我正在從shelter_doggie目錄運行終端命令,而breedData.json文件位於頂級目錄中。我也嘗試將數據導出爲csv文件並使用以下格式導入:'mongoimport -db shelterdoggie --collection breeds --type csv --file kimonoData.csv --headerline'但是我得到了這個錯誤'error validating settings:incompatible options: - 文件和位置參數(s)' – Amanda

回答

2

克萊門特的建議讓我在正確的軌道上。爲文件設置完整的文件路徑。在終端中輸入命令是這樣的:

mongoimport --db shelterdoggie --collection breeds --type json --file ~/dev/shelter_doggie/breedData.json --jsonArray 

至於我的CSV導入的嘗試,我只用了DB前一個破折號,而不是兩個破折號。我修復了這個問題併爲csv文件添加了完整的文件路徑。所以,它的工作原理,當我像這樣運行命令:

mongoimport --db shelterdoggie --collection breeds --type csv --file ~/dev/shelter_doggie/kimonoData.csv --headerline 

不知道是否有另一種方式來做到這一點不完整的文件路徑(在我最初的問題的文章指出),但至少我可以導入我的數據。

+1

如果你想使用相對路徑只是刪除 - 文件並嘗試..它會工作..mongoimport需要位置參數並將其視爲文件的值 ** mongoimport.exe -d shelterdoggie -c品種 - 類型csv .. \ something \ test1.csv --headerline ** – gsuresh92

+0

@ gsuresh92很高興知道!謝謝! – Amanda

1

對Windows中的類似問題,所以通過通過stdin例如傳送數據固定它

cat "PATH\breedData.json" | mongoimport.exe --db shelterdoggie --collection breeds --type json --file --jsonArray 

或Linux上

mongoimport --db shelterdoggie --collection breeds --type json --file --jsonArray < breedData.json 
+0

在Linux中出現「文件不存在:--jsonArray」錯誤 – Pradeepb