0

this is my mongoose schema which includes nested arrays and objects.

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

});

and this is how i insert values,

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);
 });

but the document get saved like this,

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

}

The document is not saving all the values. only one value in array is getting saved. why? is my schema is wrong? or the way i inserting values is wrong ? what i am doing wrong ? please help..

  • If you are expecting named keys then arrays don't make any sense here. Mongoose will not recognize different "sub-schema" for each array member. You "could" possibly use discriminators, but your structure does not really lend well to an array using named keys. On the other hand, you really should not be naming keys at all, and instead your `"one"` and `"two"` should be values of a standard named property rather than the name of a key. Named keys are pretty bad for databases. Better to use consistent names. – Neil Lunn Nov 11 '17 at 11:38
  • thank you bro.. i need a schema that holds all department -> classes -> division -> students count of a school. thats why i tried this , is there any alternative? –  Nov 11 '17 at 11:49
  • Nesting is not the way to do this. Lots of problems with the concept in general. Read [Updating a Nested Array with MongoDB](https://stackoverflow.com/a/23577266/2313887) and [Find in Double Nested Array MongoDB](https://stackoverflow.com/questions/29071748/find-in-double-nested-array-mongodb) in order to get some insight into the problems inherent, and the general idea of how to approach this differently. – Neil Lunn Nov 11 '17 at 12:01
  • bro `.push` is enough to push data to nested array in mongoose? –  Nov 11 '17 at 12:06

0 Answers0