title: Mongoose - Form to save model with embedded documents
body: <p>Having trouble saving an embedded array to a Mongoose model.</p>
  
  <p><strong>Please see Edit at bottom.</strong></p>
  
  <p>I have a form to create BlogPost in Express using Mongoose to store data in mongo. I can create and view new blogposts however I just added an embedded document schema Feed into the BlogPost model and I can't get Feed arrays to save from the form into the model</p>
  
  <p>code:</p>
  
  <p>BlogPosts.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/my_database');
  
  var Schema = mongoose.Schema
    , ObjectId = Schema.ObjectId;
  
  var Feeds = new Schema({
      name      : { type: String }
    , key       : { type: String }
  });
  
  var BlogPost = new Schema({
      author    : ObjectId
    , title     : { type: String, required: true, index: { unique: true } }
    , date      : { type: Date, required: true, default: Date.now }
    , feeds     : [Feeds]
  });
  
  mongoose.model('BlogPost', BlogPost);
  </code></pre>
  
  <p>web.js</p>
  
  <pre><code>...
  app.get('/blogpost/new', function(req, res) {
      res.render('blogposts/blogpost_new.jade', { locals: {
          title: 'New BlogPost'
      }
      });
  });
  
  app.post('/blogpost/new', function(req, res){
      var b = new BlogPost(req.body.b)
      b.save(function() {
        b.feeds.push();
        res.redirect('/blogposts');
      });
  });
  ...
  var BlogPost = mongoose.model('BlogPost', BlogPost);
  </code></pre>
  
  <p>Jade form</p>
  
  <pre><code>form( method="post")
    div
      div
        span Title :
        input(type="text", name="b[title]", id="editBlogPostTitle")
      div 
        span Feeds :
        ul 
          li 
            span name
            textarea( name="f[name]", rows=20, id="editBlogPostBodyName")
          li
            span key
            textarea( name="f[key]", rows=20, id="editBlogPostBodyKey")
      div#editBlogPostSubmit
        input(type="submit", value="Send")
  </code></pre>
  
  <p>If I fill out this form, the model posts and saves but the feeds data isn't there (<code>"feeds" : [ ]</code>).</p>
  
  <p>How should I properly submit the feeds data to save to the array?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>So I have managed to set up a form to save a Feed object with <code>name</code> and <code>key</code> within a BlogPost doing the following. However, this still needs to be improved to allow for multiple Feeds to be saved at the time of creating a single BlogPost. With my current solution I can only save one Feed properly. Thoughts?</p>
  
  <p>blogposts.js (just change Feeds to Feed</p>
  
  <pre><code>var Feed = new Schema({
  ...
  </code></pre>
  
  <p>web.js (just moved the push)</p>
  
  <pre><code>app.post('/blogpost/new', function(req, res){
      var b = new BlogPost(req.body.b)
      b.feeds.push(req.body.feed);
      b.save(function() {
        res.redirect('/blogposts');
  });
  </code></pre>
  
  <p>});</p>
  
  <p>form (just change feed names)</p>
  
  <pre><code>    li
          span name
          textarea( name="feed[name]", rows=20, id="editBlogPostBodyKey")
      li
          span key
          textarea( name="feed[key]", rows=20, id="editBlogPostBodyKey")
  </code></pre>
  
  <p>This saves properly, I just can't create multiple feeds within a blogpost at the time of saving. Any help greatly appreciated. thanks.</p>