嵌套数组插入在Mongo DB中失败

时间:2015-07-19 13:00:56

标签: mongodb mongodb-query

我是MongoDB的新手,并且为了我的学习目的而探索Mongo DB。所以对于专家来说,我的问题可能看起来太基本了,但是如果有人能清除我对我有帮助的疑问。

我正在尝试从命令行shell在mongo DB中插入一条记录.Below是相同的查询:

db.my_data.insert({
    "state_name":"State1",
    "building_usage_data": [{
        "area_type":"Urban",
        "building_usage": [
            {"usage_type":"House","total":1663832},
            {"usage_type":"Shop","total":352312 },
            {"usage_type":"Hotel","total":5582 },
            {"usage_type":"Hospital","total":5752},
            {"usage_type":"Temple","total":8442},
            {"usage_type":"Factory","total":3842},
            {"usage_type":"Shopping Mall","total":742},
            {"usage_type":"Cinema Hall","total":642}
        ]
    },
    {
        "area_type":"Rural,
        "building_usage": [
            {"usage_type":"House","total":1963832},
            {"usage_type":"Shop","total":302312},
            {"usage_type":"Hospital","total":2752},
            {"usage_type":"Temple","total":9442},
            {"usage_type":"Factory","total":4842},
            {"usage_type":"Resort","total":842}
        ]
    }]
});

但是上面的语法错误是失败的。在我看来,在针对building_usage_data的数组中插入第二个元素时失败了。我无法理解这次失败背后的原因。

作为替代解决方案,我已根据下面的内容更改了运行正常的查询:

db.my_data.insert({
    "state_name":"State1",
    "Urban": [
        {"usage_type":"House","total":1663832},
        {"usage_type":"Shop","total":352312 },
        {"usage_type":"Hotel","total":5582 },
        {"usage_type":"Hospital","total":5752},
        {"usage_type":"Temple","total":8442},
        {"usage_type":"Factory","total":3842},
        {"usage_type":"Shopping Mall","total":742},
        {"usage_type":"Cinema Hall","total":642}
    ],
    "Rural": [
        {"usage_type":"House","total":1963832},
        {"usage_type":"Shop","total":302312},
        {"usage_type":"Hospital","total":2752},
        {"usage_type":"Temple","total":9442},
        {"usage_type":"Factory","total":4842},
        {"usage_type":"Resort","total":842}
    ]
});

如果有人能解释我为什么我的第一个插入查询失败会有所帮助。

0 个答案:

没有答案