2017-03-29 1 views
4

Docker의 mongodb 인스턴스에 json 배열의 항목을 가져 오려고합니다. 도커없이 시도 할 때, mongoimport는 imported 7 documents로 응답하지만 고정 표시기는 imported 1 documentmongoimport를 사용하는 독점 mongodb가 json 배열을 항목으로 가져옵니다.

도커 명령 응답 :

CMD mongoimport --host mongodb --db reach-engine --collection domains --type json --file seed.json --jsonArray 

도커 MongoDB를 :

{ "_id" : ObjectId("58dc3abd7ccf9a826b2a7d34"), "items" : [ { "id" : 1, "_id" : "item1", "type" : "alert", "title" : "hello.world", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$9.00", "active" : true }, { "id" : 2, "_id" : "item2", "type" : "welcome.lol", "title" : "Item 2", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$12.00", "active" : true }, { "id" : 3, "_id" : "item3", "type" : "message", "title" : "various.domain", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$3.00", "active" : false }, { "id" : 4, "_id" : "item4", "type" : "message", "title" : "something.else", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false }, { "id" : 5, "_id" : "item5", "type" : "update", "title" : "wow.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false }, { "id" : 6, "_id" : "item6", "type" : "update", "title" : "domainname.net", "email" : "[email protected]", "message" : "cars", "createdDate" : "date", "price" : "$12.00", "active" : false }, { "id" : 7, "_id" : "item7", "type" : "update", "title" : "something.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } ] } 

지역 MongoDB를 :

{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd4"), "title" : "hello.world", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$9.00", "active" : true } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd5"), "title" : "Item 2", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$12.00", "active" : true } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd6"), "title" : "wow.lol", "email" : "[email protected]com", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd7"), "title" : "domainname.net", "email" : "[email protected]", "message" : "cars", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd8"), "title" : "something.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdd9"), "title" : "something.else", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc21cf1ebe0a21fa2fcdda"), "title" : "various.domain", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$3.00", "active" : false } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcddb"), "title" : "hello.world", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$9.00", "active" : true } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcddc"), "title" : "Item 2", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$12.00", "active" : true } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcddd"), "title" : "various.domain", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$3.00", "active" : false } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcdde"), "title" : "something.else", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcddf"), "title" : "wow.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcde0"), "title" : "domainname.net", "email" : "[email protected]", "message" : "cars", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2b7c1ebe0a21fa2fcde1"), "title" : "something.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde2"), "title" : "various.domain", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$3.00", "active" : false } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde3"), "title" : "Item 2", "email" : "[email protected]", "message" : "lol", "createdDate" : "date", "price" : "$12.00", "active" : true } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde4"), "title" : "hello.world", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$9.00", "active" : true } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde5"), "title" : "wow.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde6"), "title" : "domainname.net", "email" : "[email protected]", "message" : "cars", "createdDate" : "date", "price" : "$12.00", "active" : false } 
{ "_id" : ObjectId("58dc2d691ebe0a21fa2fcde7"), "title" : "something.lol", "email" : "[email protected]", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false } 

도커를 명령 :

mongodb_1  | 2017-03-29T21:38:09.439+0000 I COMMAND [conn1] command reach-engine.domains command: insert { insert: "domains", documents: [ { items: [ { id: 1, _id: "item1", type: "alert", title: "hello.world", email: "[email protected]", message: "", createdDate: "date", price: "$9.00", active: true }, { id: 2, _id: "item2", type: "welcome.lol", title: "Item 2", email: "[email protected]", message: "lol", createdDate: "date", price: "$12.00", active: true }, { id: 3, _id: "item3", type: "message", title: "various.domain", email: "[email protected]", message: "lol", createdDate: "date", price: "$3.00", active: false }, { id: 4, _id: "item4", type: "message", title: "something.else", message: "", createdDate: "date", price: "$12.00", active: false }, { id: 5, _id: "item5", type: "update", title: "wow.lol", email: "[email protected]", message: "", createdDate: "date", price: "$12.00", active: false }, { id: 6, _id: "item6", type: "update", title: "domainname.net", email: "[email protected]", message: "cars", createdDate: "date", price: "$12.00", active: false }, { id: 7, _id: "item7", type: "update", title: "something.lol", email: "[email protected]", message: "", createdDate: "date", price: "$12.00", active: false } ] } ], writeConcern: { getLastError: 1, w: 1 }, ordered: false } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } } } protocol:op_query 250ms 

무엇이 원인 일 수 있습니까?

+1

이 문제를 발견 할 때 사용하고있는 Dockerfile/docker-compose 구조를 알려주십시오. –

+1

로컬에 설치 한 MongoDB의 버전과 도커 컨테이너에 설치 한 버전을 확인할 수 있습니까? 또한 JSON 파일의 스 니펫을 볼 수 있습니까? –

답변

3

docker mongo 서버 동작 (3.x 버전 일 수도 있음)이 정확합니다. 즉, items 인 문서를 삽입 된 배열로 삽입하는 것입니다.

로컬 mongo 동작 (2.x 버전 일 수도 있음)은 JSON 형식 문제로 인해 발생합니다.

게시물에 제공된 정보를 사용하여 문제를 재현하기 위해 아래 단계를 수행했습니다.

은 모두 2.6 및 3.4 버전에서 정확히 같은 sample1.json에게

{items:[{item:1},{item:2},{item:3}]}

작품을 고려하십시오.

컬렉션

{ "_id" : ObjectId("58e44c281a573a105fe10d47"), "items" : [ { "item" : 1 }, { "item" : 2 }, { "item" : 3 } ] }

{"items":[{"item":1}, {"item":2}, {"item":3}]}

그것은 3.4 버전에 상기와 동일하게 작동하지만 2.6에서 다르게 작동 sample2.json (CRLF있다) 고려하십시오.

첫 번째 요소를 삽입하는 동안 실패하지만 다음 두 요소를 2.6 버전의 별도 문서로 계속 삽입합니다.

명령

mongoimport --db test --collection sample --type json --file sample2.json

로그인

exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Bad characters in value: offset:21 of:{"items":[{"item":1}, 2017-04-04T21:03:13.869-0500 imported 2 objects encountered 1 error(s)

컬렉션

{ "_id" : ObjectId("58e4506169a04993a1697f29"), "item" : 2 } 
{ "_id" : ObjectId("58e4506169a04993a1697f2a"), "item" : 3 } 

수정 :

당신은 문서로 각 요소를 삽입하고 --jsonArray 플래그를 사용하려면 아래 (JSON 형식은 중요하지 않습니다)에 JSON을 업데이트해야합니다. 2.6과 3.4에서 모두 똑같이 작동합니다.

고려 sample.json

[{"item":1}, {"item":2}, {"item":3}]

mongoimport --db test --collection sample --type json --file sample.json --jsonArray

로그인

2017-04-04T21:19:34.018-0500 imported 3 objects

골 leock

{ "_id" : ObjectId("58e454362a9c7ef0228df346"), "item" : 1 } 
{ "_id" : ObjectId("58e454362a9c7ef0228df347"), "item" : 2 } 
{ "_id" : ObjectId("58e454362a9c7ef0228df348"), "item" : 3 } 
+0

Mongo 3.4.0에서 --jsonArray를 사용하여 동일한 작업을하고있는 것 같습니다. 여전히 다른 JSON으로 테스트 중이며 동일한 문제가 발생하고 있습니다. ( – iamwhitebox

+0

항목 키를 제거 했습니까? 최신 json 파일로 게시물을 업데이트하십시오. 또한 로컬 vs 도커 인스턴스에서 mongo 버전을 확인 했습니까? – Veeram

관련 문제