development

JSON 파일의 몽고 임포트

big-blog 2020. 6. 9. 07:42
반응형

JSON 파일의 몽고 임포트


약 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"}},

각 레코드는 JSON 파일에서 한 줄에 있으며 줄 바꿈은 모든 레코드 끝에 있습니다. 따라서 문서의 각 줄은 "{jobID :"로 시작합니다. 다음 명령을 사용하여 몽고 데이터베이스로 가져 오려고합니다.

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

그러나 다음과 같은 오류가 발생합니다.

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

나는 문제가 무엇인지 모른다. 누군가 솔루션을 추천 할 수 있습니까?


다음 쿼리를 사용하여 오류를 해결할 수있었습니다.

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

잘하면 이것은 누군가에게 도움이됩니다.


이 시도,

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

예,

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

문제는 날짜 형식 때문입니다.

아래와 같이 수정 된 날짜와 동일한 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"}}

도움이 되었기를 바랍니다


mongoimport사용 하면 동일한 결과를 얻을 수 있습니다

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

어디,

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

--drop is drop the collection if already exist.


console:

mongoimport -d dbName -c collectionName dataFile.js 

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.


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

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

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

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

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:

show databases

This command works where no collection is specified .

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

Mongo shell after executing the command

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

I tried something like this and it actually works:

mongoimport --db dbName --file D:\KKK\NNN\100YWeatherSmall.data.json

This works with me when db with usrname and password

mongoimport --db YOUR_DB --collection MyCollection --file /your_path/my_json_file.json -u my_user -p my_pass

db without username password please remove -u my_user -p my_pass

My sample json

{ 
    "_id" : ObjectId("5d11c815eb946a412ecd677d"), 
    "empid" : NumberInt(1), 
    "name" : "Rahul"
}
{ 
    "_id" : ObjectId("5d11c815eb946a412ecd677e"), 
    "empid" : NumberInt(2), 
    "name" : "Rahul"
}

If you try to export this test collection:

> db.test.find()
{ "_id" : ObjectId("5131c2bbfcb94ddb2549d501"), "field" : "Sat Mar 02 2013 13:13:31 GMT+0400"}
{"_id" : ObjectId("5131c2d8fcb94ddb2549d502"), "field" : ISODate("2012-05-31T11:59:39Z")}

with mongoexport (the first date created with Date(...) and the second one created with new Date(...) (if use ISODate(...) will be the same as in the second line)) so mongoexport output will be looks like this:

{ "_id" : { "$oid" : "5131c2bbfcb94ddb2549d501" }, "field" : "Sat Mar 02 2013 13:13:31 GMT+0400" }
{ "_id" : { "$oid" : "5131c2d8fcb94ddb2549d502" }, "field" : { "$date" : 1338465579000 } }

So you should use the same notation, because strict JSON doesn't have type Date( <date> ).

Also your JSON is not valid: all fields name must be enclosed in double quotes, but mongoimport works fine without them.

You can find additional information in mongodb documentation and here.


Solution:-

mongoimport --db databaseName --collection tableName --file filepath.json

Example:-

Place your file in admin folder:-

C:\Users\admin\tourdb\places.json

Run this command on your teminal:-

mongoimport --db tourdb --collection places --file ~/tourdb/places.json

Output:-

admin@admin-PC MINGW64 /
$ mongoimport --db tourdb --collection places --file ~/tourdb/places.json
2019-08-26T14:30:09.350+0530 connected to: localhost
2019-08-26T14:30:09.447+0530 imported 10 documents

For more link


this should work inside the mongoshell

mongoimport -d databaseName -c collectionName --drop fileName.json

if you did not launch the shell in the required directory, you've to specify the file along with its path.

참고URL : https://stackoverflow.com/questions/15171622/mongoimport-of-json-file

반응형