如何使用mongoose将嵌套对象插入mongo db? (对象包含多个数组)

时间:2017-11-11 10:02:16

标签: arrays json mongodb express mongoose

这是我的猫鼬schema ,其中包含嵌套数组和对象。

    classes:[{
    LP:[
        {
            one:[
                {
                    division:String,
                    strength:String
                }
            ]
        }
    ]
}]

});

这就是我插入值的方式,

var SchoolDetails=new school_details({
classes:[
    {
        LP:[
            {
                ONE:[
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    }
                ]
            },
            {
                TWO:[
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    }
                ]
            }

        ],
        UP:[
            {
                ONE:[
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    }
                ]
            },
            {
                TWO:[
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    },
                    {
                        division: "A",
                        stregth: "20"
                    }
                ]
            }

        ]
    }
]

});
SchoolDetails.save(function (err,data) {
if(err){
    console.log(err);
 }
console.log("School Details ;"+data);
 });

但文档会像这样保存,

{
    "_id" : ObjectId("5a06ddb228603b2888d3076e"),
    "classes" : [
            {
                    "_id" : ObjectId("5a06ddb228603b2888d3076f"),
                    "LP" : [
                            {
                                    "_id" : ObjectId("5a06ddb228603b2888d30771"),
                                    "one" : [ ]
                            },
                            {
                                    "_id" : ObjectId("5a06ddb228603b2888d30770"),
                                    "one" : [ ]
                            }
                    ]
            }
    ],
    "__v" : 0

}

文档未保存所有值。数组中只有一个值被保存。为什么吗 我的架构是错的吗? 或者我插入值的方式是错误的? 我做错了什么?请帮忙..

0 个答案:

没有答案