web-dev-qa-db-ja.com

Mongoimportはjson配列をコレクションのキーとしてではなくコレクションとしてインポートします

Mongoimportを使用してデータのjson配列をデータベースにシードしようとしていますが、データがmongoコレクションに到達すると、次のようにコレクションオブジェクトにキーとしてインポートされます。

[![ここに画像の説明を入力してください] [1]] [1]

「items」は私のjsonファイルであり、常に「items」として表示されます。親配列をインポートしようとしている配列にしたいのですが、これは意味がありますか?

更新

この例を参照してください。最初の画像は、mongoimportがこのオブジェクトの配列をインポートする方法です。

{ "_id" : ObjectId("58dc01ecec116d4c9039e47c"), "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 } ] }

配列全体を「item」オブジェクトとして扱い、配列であるアイテム「items」にキーがあることに注意してください。データは次のようになります。

{ "_id" : ObjectId("58dc027a2c74df002a957281"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:42.227Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027b2c74df002a957282"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:43.574Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027b2c74df002a957283"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:43.708Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027b2c74df002a957284"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:43.855Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027b2c74df002a957285"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:43.994Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027c2c74df002a957286"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:44.128Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027c2c74df002a957287"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:44.263Z"), "active" : true, "__v" : 0 }
{ "_id" : ObjectId("58dc027c2c74df002a957288"), "price" : "asdf", "message" : "asdf", "email" : "aasfd", "title" : "asdf", "dateCreated" : ISODate("2017-03-29T18:52:44.391Z"), "active" : true, "__v" : 0 }

配列内の各アイテムがmongoで「アイテム」として作成され、それぞれが独自のObjectIDを持つ場合、それ以外の場合はCRUDアプリケーションには役立ちません。

Docker MongoDBログ:

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
6
iamwhitebox

インポートに使用しているコマンドを知っておくと便利ですが、このコマンドを使用して次のJSONをインポートしてデータベースを作成しました。

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

--jsonArrayフラグを使用していることを確認してください


example.json

[
    {
        "color": "red",
        "value": "#f00"
    },
    {
        "color": "green",
        "value": "#0f0"
    },
    {
        "color": "blue",
        "value": "#00f"
    },
    {
        "color": "cyan",
        "value": "#0ff"
    },
    {
        "color": "Magenta",
        "value": "#f0f"
    },
    {
        "color": "yellow",
        "value": "#ff0"
    },
    {
        "color": "black",
        "value": "#000"
    }
]
20
daviscodesbugs