Mongoimport导入json数组作为集合而不是集合

时间:2017-03-28 23:01:02

标签: mongodb mongoimport

我正在尝试使用mongoimport为json数据组播种数据库,但是当数据到达mongo集合时,它会像集合对象一样导入到集合对象中:

[![在此处输入图像说明] [1]] [1]

“items”是我的json文件,它总是显示为“items”,我希望父数组是我试图导入的数组,这有意义吗?

更新

请看这个例子,第一个图像是mongoimport如何导入这个对象数组:

{ "_id" : ObjectId("58dc01ecec116d4c9039e47c"), "items" : [ { "id" : 1, "_id" : "item1", "type" : "alert", "title" : "hello.world", "email" : "something@something.com", "message" : "", "createdDate" : "date", "price" : "$9.00", "active" : true }, { "id" : 2, "_id" : "item2", "type" : "welcome.lol", "title" : "Item 2", "email" : "something@something.com", "message" : "lol", "createdDate" : "date", "price" : "$12.00", "active" : true }, { "id" : 3, "_id" : "item3", "type" : "message", "title" : "various.domain", "email" : "something@something.com", "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" : "something@something.com", "message" : "", "createdDate" : "date", "price" : "$12.00", "active" : false }, { "id" : 6, "_id" : "item6", "type" : "update", "title" : "domainname.net", "email" : "something@something.com", "message" : "cars", "createdDate" : "date", "price" : "$12.00", "active" : false }, { "id" : 7, "_id" : "item7", "type" : "update", "title" : "something.lol", "email" : "something@something.com", "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中的“item”,每个项目都有自己的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: "something@something.com", message: "", createdDate: "date", price: "$9.00", active: true }, { id: 2, _id: "item2", type: "welcome.lol", title: "Item 2", email: "something@something.com", message: "lol", createdDate: "date", price: "$12.00", active: true }, { id: 3, _id: "item3", type: "message", title: "various.domain", email: "something@something.com", 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: "something@something.com", message: "", createdDate: "date", price: "$12.00", active: false }, { id: 6, _id: "item6", type: "update", title: "domainname.net", email: "something@something.com", message: "cars", createdDate: "date", price: "$12.00", active: false }, { id: 7, _id: "item7", type: "update", title: "something.lol", email: "something@something.com", 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 个答案:

答案 0 :(得分:14)

知道用于导入的命令会很有用,但我刚创建了一个数据库,并使用以下命令导入了以下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"
    }
]