2013-05-09 47 views
1

我已經在我的系統的D:\驅動器中創建一個demodb.json文件(具有10個文件),並保持它的demodb.json如何導入JSON文件包含超過200個文件到MongoDB的

內容是:

[{"ID": 1,"Name": "Kabul","CountryCode": "AFG","District": "Kabol","Population": 1780000}, {"ID": 2,"Name": "Qandahar","CountryCode": "AFG","District": "Qandahar","Population": 237500}, {"ID": 3,"Name": "Herat","CountryCode": "AFG","District": "Herat","Population": 186800}, {"ID": 4,"Name": "Mazar-e-Sharif","CountryCode": "AFG","District": "Balkh","Population": 127800}, {"ID": 5,"Name": "Amsterdam","CountryCode": "NLD","District": "Noord-Holland","Population": 731200}, {"ID": 6,"Name": "Rotterdam","CountryCode": "NLD","District": "Zuid-Holland","Population": 593321}, {"ID": 7,"Name": "Haag","CountryCode": "NLD","District": "Zuid-Holland","Population": 440900}, {"ID": 8,"Name": "Utrecht","CountryCode": "NLD","District": "Utrecht","Population": 234323}, {"ID": 9,"Name": "Eindhoven","CountryCode": "NLD","District": "Noord-Brabant","Population": 201843}, {"ID": 10,"Name": "Tilburg","CountryCode": "NLD","District": "Noord-Brabant","Population": 193238}] 

我的MongoDB的位置是:D:\SanMongoDB

我所有的數據庫和集合存儲在D:\SanMongoDB\MongoData

demodb的0

內容 - >城市收集如下

{ "_id":ObjectId("51892b40842d69f5fa21962f"), 
    "Name": "India", 
    "CountryCode": "IND", 
    "District": "ODISHA", 
    "Population": 2050000 
} 

我開始mongod的服務器,然後打開蒙戈客戶 在蒙戈客戶我做了以下內容:

使用demodb的 mongoimport -d demodb的-c城市--type JSON --file demodb.json --jsonarray

但我發現如下錯誤

Thu May 09 08:21:57.861 Javascript execution failed:SyntaxError:Unexpected identifier

請提出我需要做什麼,以便我可以輕鬆地將Json文件導入到Mongodb的現有數據庫中。

+0

這是因爲正確的ID是'_id',而不是'ID'。所以,'意外的標識符'。 – stackoverflowery 2013-05-09 03:13:07

回答

0

假設你的東西在file.json中,請嘗試sed -e 's/ID/_id/g;s/\([^:]*\):\([^,}]*\)/"\1":"\2"/g' file.json | mongoimport --collection collection-name。你可以得到sed for Windows from GNUWin32。讓我知道你是否還有其他問題。

這裏,s/ID/_id/g每一個字,使您的文件valid JSON代用品ID_id每次出現文件file.json和第二報價英寸經過兩次轉換,mongoimport運行良好。