mongodb에 json 파일 삽입
나는 mongodb에서 처음입니다. Windows에 mongodb를 설치 한 후 다음 명령을 사용하여 간단한 json 파일을 삽입하려고합니다.
C:\>mongodb\bin\mongoimport --db test --collection docs < example2.json
다음과 같은 오류가 발생합니다.
connected to: 127.0.0.1
Fri Oct 18 09:05:43.749 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:43
Fri Oct 18 09:05:43.750
Fri Oct 18 09:05:43.750 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:42
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:44
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 check 0 0
Fri Oct 18 09:05:43.752 imported 0 objects
Fri Oct 18 09:05:43.752 ERROR: encountered 6 error(s)s
example2.file
{"FirstName": "Bruce", "LastName": "Wayne",
"Email": "bwayne@Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox",
"Email": "lfox@Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson",
"Email": "dgrayson@Wayneenterprises.com"}
새 json 파일을 mongodb로 가져 오려면 어떻게해야합니까?
사용하다
mongoimport --jsonArray --db test --collection docs --file example2.json
개행 문자 때문에 아마도 엉망이 될 것입니다.
아래 명령이 나를 위해 일했습니다.
mongoimport --db test --collection docs --file example2.json
Email
각 문서에서 속성 앞에 추가 개행 문자를 제거했을 때 .
example2.json
{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bwayne@Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox", "Email": "lfox@Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson", "Email": "dgrayson@Wayneenterprises.com"}
이것은 나를 위해 일했습니다-(몽고 쉘에서)
var file = cat('./new.json'); # file name
use testdb # db name
var o = JSON.parse(file); # convert string to JSON
db.forms.insert(o) # collection name
JSON 파일을 가져올 때 아래 명령 사용
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
mongoimport --jsonArray -d DatabaseN -c collectionName /filePath/filename.json
다음 두 가지 방법이 잘 작동합니다.
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs < example2.json
컬렉션이 특정 사용자 아래에있는 경우 다음을 사용할 수 있습니다. -u -p --authenticationDatabase
MS Windows에서 mongoimport 명령은 mongodb 명령 프롬프트가 아닌 일반 Windows 명령 프롬프트에서 실행해야합니다.
It happened to me couple of weeks back. The version of mongoimport was too old. Once i Updated to latest version it ran successfully and imported all documents.
This solution is applicable for Windows machine.
MongoDB needs data directory to store data. Default path is
C:\data\db
. In case you don't have the data directory, create one in your C: drive. (P.S.: data\db means there is a directory named 'db' inside the directory 'data')Place the json you want to import in this path:
C:\data\db\
.Open the command prompt and type the following command
mongoimport --db databaseName --collections collectionName --file fileName.json --type json --batchSize 1
Here,
- databaseName : your database name
- collectionName: your collection name
- fileName: name of your json file which is in the path C:\data\db
- batchSize can be any integer as per your wish
In MongoDB To insert Json array data from file(from particular location from a system / pc) using mongo shell command. While executing below command, command should be in single line.
var file = cat('I:/data/db/card_type_authorization.json'); var o = JSON.parse(file); db.CARD_TYPE_AUTHORIZATION.insert(o);
JSON File: card_type_authorization.json
[{
"code": "visa",
"position": 1,
"description": "Visa",
"isVertualCard": false,
"comments": ""
},{
"code": "mastercard",
"position": 2,
"description": "Mastercard",
"isVertualCard": false,
"comments": ""
}]
참고URL : https://stackoverflow.com/questions/19441228/insert-json-file-into-mongodb
'Programing' 카테고리의 다른 글
프로그래밍 방식으로 드롭 다운 목록 선택 항목 설정 (0) | 2020.11.13 |
---|---|
WebP 지원 감지 (0) | 2020.11.13 |
PHP에서 이미지 출력 (0) | 2020.11.13 |
Redis 세트 대 해시 (0) | 2020.11.12 |
IEnumerable은 왜 (0) | 2020.11.12 |