如何导入JSON文件包含超过200个文件到MongoDB的

问题描述:

我已经在我的系统的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

假设你的东西在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运行良好。

不是100%肯定,但我认为它应该是这样的:

开始的mongod

使用demodb的

mongoimport -d [DBNAME] -c [集合名] --demodb.json - -jsonarray

一些示例

http://www.mkyong.com/mongodb/mongodb-import-and-export-example/