title: How to create nested object in MongoDB schema
body: <p>I currently have the following schema for a MongoDB document which is supposed to save user data:</p>
  <pre><code>var userSchema =  new mongoose.Schema({
    username: {type: String, unique : true},
    password: {type: String},
    firstname: String,
    lastname: String,
    sketches: 
                [ 
                  {name: String, 
                   sketch: Array}
                ]
  </code></pre>
  <p>The sketches attribute needs to be an array objects where each object associates the name of a sketch and an array which holds the sketch data. For some reason, the schema ends up being created as the following:</p>
  <pre><code>{
      &quot;__v&quot; : 1,
      &quot;_id&quot; : ObjectId(&quot;57c4d7693aa85cea2acf4d4d&quot;),
      &quot;firstname&quot; : &quot;test&quot;,
      &quot;lastname&quot; : &quot;name&quot;,
      &quot;password&quot; : &quot;password123&quot;,
      &quot;sketches&quot; : [ 
          {
              &quot;sketch&quot; : []
          }
      ],
      &quot;username&quot; : &quot;testname&quot;
  }
  </code></pre>
  <p>I'm not exactly sure the correct format for creating nested objects in MongoDB but I was assuming that it would be the same as it would for JSON. How should the schema be structured to yield an array of objects.</p>
  <h2>EDIT:</h2>
  <p><strong>web service to insert into document from PUT request:</strong></p>
  <pre><code>app.route(&quot;/addSketch/:username&quot;).put(function(req, res, next) {
    var user_name = req.params.username;
    User.findOne({username:user_name},function(err,foundObject){
      if(err){
        console.log(&quot;error&quot;);
        res.status(500).send();
      }
      else{
        if(!foundObject){
          res.status(404).send();
        }
        else{
          
          if(req.body.strokes &amp;&amp; req.body.sketchName){
            var sketchObj = [];
            sketchObj[req.body.sketchName] = req.body.strokes;
            foundObject.sketches.push(req.body.sketchData);
          }
          foundObject.save(function(err,updatedObject){
            if(err){
              console.log(err);
              res.status(500).send();
            }
            else{
              res.send(updatedObject);
            }
          });
        }
      }
  
    });
  
  
    console.log('saving on server');
     var form = formidable.IncomingForm();
  
     console.log(form);
     console.log('the type of the request received is', (typeof req));
  
    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {&quot;content-type&quot;: &quot;text/plain&quot;});
        res.write('received upload:\n\n');
      var name = fields.name;
      var newSketch = new SavedSketch();
        newSketch.name = name;
        newSketch.sketchData =  fields.value;
        newSketch.save(function(err,savedObject){
          if(err){
                 console.log(err);
                 res.status(500).json({status:'failure'})
              }
              else{
                console.log(&quot;ID: &quot; + fields.value.id + &quot; strokeData:&quot; + fields.value.strokes);
                 res.json({status: 'success'});
              } 
        });
  
           res.end();
    });
    });
  </code></pre>