2017-03-29 14 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]", "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 /ドッキングウィンドウ・コン構造に私を指してくださいもらえますか? –

+1

ローカルにインストールしたMongoDBのバージョンと、ドッカーのコンテナにインストールしたバージョンを確認できますか?また、JSONファイルのスニペットを見ることは可能ですか? –

答えて

3

docker mongoサーバーの動作(おそらく3.xバージョン)は正しいです。すなわち、埋め込み配列としてitemsという単一のドキュメントを挿入します。

ローカルのmongoの動作(おそらく2.xバージョン)は、JSONの書式設定の問題が原因です。

投稿に記載されている情報を使用して問題を再現するために、以下の手順を実行しました。

sample1.json

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

は両方2.6と3.4のバージョンでは正確に同じ動作を検討してください。

コレクション

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

は、それが3.4バージョンのため、上記と同様に動作しますが、2.6で動作が異なりますsample2.jsonCRLFを持っている)

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

を考えてみましょう。

最初の要素を挿入する間に失敗しますが、次の2つの要素を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

コルを考えてみましょうlection

{ "_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ファイルまた、ローカル対ドッカーのインスタンスでmongoのバージョンを確認しましたか – Veeram

関連する問題