json文件的mongoimport

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15171622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:46:01  来源:igfitidea点击:

Mongoimport of json file

jsonmongodbimportmongoimport

提问by amber4478

I have a json file consisting of about 2000 records. Each record which will correspond to a document in the mongo database is formatted as follows:

我有一个由大约 2000 条记录组成的 json 文件。与 mongo 数据库中的文档对应的每条记录的格式如下:

{jobID:"2597401",
account:"XXXXX",
user:"YYYYY",
pkgT:{"pgi/7.2-5":{libA:["libpgc.so"],flavor:["default"]}},     
startEpoch:"1338497979",
runTime:"1022",
execType:"user:binary",
exec:"/share/home/01482/XXXXX/appker/ranger/NPB3.3.1/NPB3.3-MPI/bin/ft.D.64",
numNodes:"4",
sha1:"5a79879235aa31b6a46e73b43879428e2a175db5",
execEpoch:1336766742,
execModify: new Date("Fri May 11 15:05:42 2012"),
startTime: new Date("Thu May 31 15:59:39 2012"),
numCores:"64",
sizeT:{bss:"1881400168",text:"239574",data:"22504"}},

Each record is on a single line in the JSON file, and the only line breaks are at the end of every record. Therefore, each line in the document starts with "{jobID:"... I am trying to import these into a mongo database using the following command:

每条记录都在 JSON 文件中的一行中,唯一的换行符位于每条记录的末尾。因此,文档中的每一行都以“{jobID:”开头...我正在尝试使用以下命令将这些导入到 mongo 数据库中:

mongoimport --db dbName --collection collectionName --file fileName.json

However, I get the following error:

但是,我收到以下错误:

Sat Mar  2 01:26:12 Assertion: 10340:Failure parsing JSON string near: ,execModif
0x10059f12b 0x100562d5c 0x100562e9c 0x10025eb98 0x10000e643 0x100010b60 0x10055c4cc 0x1000014b7    
0x100001454 
 0   mongoimport                         0x000000010059f12b _ZN5mongo15printStackTraceERSo + 43
 1   mongoimport                         0x0000000100562d5c _ZN5mongo11msgassertedEiPKc + 204
 2   mongoimport                         0x0000000100562e9c _ZN5mongo11msgassertedEiRKSs + 12
 3   mongoimport                         0x000000010025eb98 _ZN5mongo8fromjsonEPKcPi + 1576
 4   mongoimport                         0x000000010000e643          
                                         _ZN6Import8parseRowEPSiRN5mongo7BSONObjERi + 2739
 5   mongoimport                         0x0000000100010b60 _ZN6Import3runEv + 7376
 6   mongoimport                         0x000000010055c4cc _ZN5mongo4Tool4mainEiPPc + 5436
 7   mongoimport                         0x00000001000014b7 main + 55
 8   mongoimport                         0x0000000100001454 start + 52
Sat Mar  2 01:26:12 exception:BSON representation of supplied JSON is too large: Failure parsing    
    JSON string near: ,execModif
Sat Mar  2 01:26:12 
Sat Mar  2 01:26:12 imported 0 objects
Sat Mar  2 01:26:12 ERROR: encountered 1941 errors

I do not know what the problem is. Can someone recommend a solution?

我不知道是什么问题。有人可以推荐一个解决方案吗?

回答by amber4478

I was able to fix the error using the following query:

我能够使用以下查询修复错误:

mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray

Hopefully this is helpful to someone.

希望这对某人有帮助。

回答by Srivatsa N

try this,

尝试这个,

mongoimport --db dbName --collection collectionName <fileName.json

Example,

例子,

mongoimport --db foo --collection myCollections < /Users/file.json
connected to: *.*.*.*
Sat Mar  2 15:01:08 imported 11 objects

Issue is because of you date format.

问题是因为你的日期格式。

I used same JSON with modified date as below and it worked

我使用了如下修改日期的相同 JSON 并且它有效

{jobID:"2597401",
account:"XXXXX",
user:"YYYYY",
pkgT:{"pgi/7.2-5":{libA:["libpgc.so"],flavor:["default"]}},     
startEpoch:"1338497979",
runTime:"1022",
execType:"user:binary",
exec:"/share/home/01482/XXXXX/appker/ranger/NPB3.3.1/NPB3.3-MPI/bin/ft.D.64",
numNodes:"4",
sha1:"5a79879235aa31b6a46e73b43879428e2a175db5",
execEpoch:1336766742,
execModify:{"$date" : 1343779200000},
startTime:{"$date" : 1343779200000},
numCores:"64",
sizeT:{bss:"1881400168",text:"239574",data:"22504"}}

hope this helps

希望这可以帮助

回答by KARTHIKEYAN.A

Using mongoimportyou can able to achieve the same

使用mongoimport您可以实现相同的目标

mongoimport --db test --collection user --drop --file ~/downloads/user.json

where,

在哪里,

test - Database name
user - collection name
user.json - dataset file

--dropis drop the collection if already exist.

--drop如果已经存在,则删除该集合。

回答by Andrey

console:

安慰:

mongoimport -d dbName -c collectionName dataFile.js 

回答by Ravi Hirani

I have used below command for export DB

我已使用以下命令导出数据库

mongodump --db database_name --collection collection_name

and below command worked for me to import DB

和下面的命令对我来说有效导入数据库

mongorestore --db database_name path_to_bson_file

回答by Robert Grutza

Your syntax appears completely correct in:

您的语法在以下方面完全正确:

mongoimport --db dbName --collection collectionName --file fileName.json

Make sure you are in the correct folder or provide the full path.

确保您位于正确的文件夹中或提供完整路径。

回答by Nija I Pillai

Run the import command in another terminal. (not inside mongo shell.)

在另一个终端中运行导入命令。(不在 mongo shell 内。)

mongoimport --db test --collection user --drop --file ~/downloads/user.json

回答by Noha Salah

In windows you can use your Command Prompcmd cmd, in Ubuntu you can use your terminalby typing the following command:

在 Windows 中,您可以使用 Command Prompcmd cmd,在 Ubuntu 中,您可以terminal通过键入以下命令来使用:

mongoimport  -d  your_database_name  -c  your_collection_name  /path_to_json_file/json_file_name.json

then when you open your mongo shell, you will find to check your database_name when running this command:

然后当你打开你的 mongo shell 时,你会发现在运行这个命令时检查你的 database_name:

show databases

回答by Abhi

This command works where no collection is specified .

此命令适用于未指定集合的​​情况。

mongoimport --db zips "\MongoDB 2.6 Standard\mongodb\zips.json"

Mongo shell after executing the command

执行命令后的mongo shell

connected to: 127.0.0.1
no collection specified!
using filename 'zips' as collection.
2014-09-16T13:56:07.147-0400 check 9 29353
2014-09-16T13:56:07.148-0400 imported 29353 objects

回答by tyne

I tried something like this and it actually works:

我试过这样的事情,它确实有效:

mongoimport --db dbName --file D:\KKK\NNN0YWeatherSmall.data.json