我创建了一个demodb.json
文件(包含10个文档),并将其保存在我的系统的D:\
驱动器中
demodb.json
的内容是:
[{"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的内容 - >城市集合如下
{ "_id":ObjectId("51892b40842d69f5fa21962f"),
"Name": "India",
"CountryCode": "IND",
"District": "ODISHA",
"Population": 2050000
}
我启动了Mongod服务器然后打开mongo客户端 在mongo客户端,我做了以下事情:
使用demodb mongoimport -d demodb -c city --type json --file demodb.json --jsonarray
但我发现错误如下
Thu May 09 08:21:57.861 Javascript execution failed:SyntaxError:Unexpected identifier
请建议我需要做什么,这样我就可以轻松地将Json文件导入到现有的Mongodb数据库中。
答案 0 :(得分: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
会在文件ID
中用_id
替换每个file.json
,而第二个引用每个单词,以使您的文件valid JSON。在两次转换之后,mongoimport工作正常。
答案 1 :(得分:0)
不是100%肯定,但我认为它应该是这样的:
启动mongod
使用demodb
mongoimport -d [dbname] -c [collectionname] --demodb.json --jsonarray
一些例子
http://www.mkyong.com/mongodb/mongodb-import-and-export-example/