Mongoose model access nested array

150 views
Skip to first unread message

Abhiram Talluri

unread,
Jun 20, 2014, 9:03:37 AM6/20/14
to mongod...@googlegroups.com
Hie,

I have a schema this way.
{

            username: { type: String, required: true, index: { unique: true } },
            password: { type: String, required: true },
            email : { type: String},
            countryname : { type: String},
            country_id : Schema.Types.ObjectId,
            children: [

                    id: Schema.Types.ObjectId,
                    child_name: String,
                    grade_id: Schema.Types.ObjectId,
                    grade :  { type: String },
                    total_gold: { type: Number},
                    total_diamonds:{ type: Number},
                    total_hearts: { type: Number},
                    subjects: []
            ]
}

How can i access subjects array which is internally stored in children array using mongoose?


William Berkeley

unread,
Jul 7, 2014, 11:07:28 AM7/7/14
to mongod...@googlegroups.com
Hi Abhiram. The syntax you are looking for to access the subjects array of the ith element of the children array, assuming a document of the type above stored in a var user, is

user.children[i].subjects

e.g., in the above,

user.children[0].subjects.length == 0

evaluates to true.

-Will
Reply all
Reply to author
Forward
0 new messages