title: Getting nested array references using mongoose schema?
body: <p>I have a schema of the format given below.</p>
  <pre class="lang-js prettyprint-override"><code>var parentSchema = mongoose.Schema({
    id: Schema.Types.ObjectId,
    username: { type: String, required: true, index: { unique: true } },
    password: { type: String, required: true },
    country_id: Schema.Types.ObjectId,
    children: [
      {
        id: Schema.Types.ObjectId,
        child_name: String,
        standard_id: Schema.Types.ObjectId,
        total_gold: { type: Number },
        total_diamonds: { type: Number },
        total_hearts: { type: Number },
        subjects: [
          {
            subject_name: String,
            grade_name: String,
            games: [{ id: String, name: String }],
          },
        ],
      },
    ],
  });
  
  module.exports = mongoose.model(&quot;Parent&quot;, parentSchema);
  </code></pre>
  <p><code>Parent</code> is the model and <code>children</code> is an array of objects which internally contains an array called <code>subjects</code>. Subjects also contain an array called <code>games</code>.</p>
  <p>How can I push data into subjects and games array using the model?
  This is what I am doing.</p>
  <pre class="lang-js prettyprint-override"><code>Parent.findOne({ username: req.body.username }, function (err, parent) {
    if (err) res.json(err);
    // if the user is found, then log them in
    if (parent) {
      res.json(parent);
    } else {
      var parent = new Parent();
      parent.username = req.body.username;
      parent.password = req.body.password;
      parent.children.push({
        child_name: req.body.childname,
        total_gold: 0,
        total_diamonds: 0,
        total_hearts: 0,
      });
    }
  });
  </code></pre>
  <p>How can I push the data into subjects array and game array?</p>