1

我创建了一个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-->city集合的内容如下

{  "_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 数据库。

4

2 回答 2

0

假设你的东西在 file.json 中,试试sed -e 's/ID/_id/g;s/\([^:]*\):\([^,}]*\)/"\1":"\2"/g' file.json | mongoimport --collection collection-name. 您可以从 GNUWin32 获得适用于 Windows 的 sed。如果您还有其他问题,请告诉我。

在这里,替换文件中s/ID/_id/g每次出现的IDwith并且第二个引用每个单词以使您的文件有效 JSON。两次转换后, mongoimport 工作正常。_idfile.json

于 2013-05-09T03:18:29.920 回答
0

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

启动 mongod

使用演示数据库

mongoimport -d [dbname] -c [collectionname] --demodb.json --jsonarray

一些例子

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

于 2014-02-24T03:17:27.397 回答