title: Updating embedded document Mongoose - existing instance save/update etc
body: <p>I can't seem to be able to update/add an array of embedded documents to a parent document for the life of me.  </p>
  
  <p>Please excuse the lengthy script, but it just wasn't going to help me or be possible to assist if I didn't include all of the code. Feel free to offer any other Node advice.</p>
  
  <p>I have tried hundreds of variations and have looked in all possible resources to get this to work, or do something similar, but have been unsuccessful for a few hours now.  I would greatly appreciate any help getting this working.  Please let me know if there's something I missed.</p>
  
  <p>This answer: <a href="https://stackoverflow.com/questions/7035092/how-to-update-embedded-document-in-mongoose">How to update embedded document in mongoose?</a> was the closest to helping me (because I already had the company/instance) but alas, I've yet to get it working.  </p>
  
  <p>Thanks for any help!</p>
  
  <pre><code>Node --version v0.10.21        
  mongodb@1.3.19
      │ ├── bson@0.2.2
      │ └── kerberos@0.0.3
      ├── mongoose@3.6.20
  
  
      var http = require('http');
      var db = require('../config/db') //Connects to DB successfully
      var async = require('async');
      var request = require('request');
      var download = require('./download');
      request = require('request');
      var cheerio = require('cheerio');
      var mongoose = require('mongoose'),
          Schema = mongoose.Schema,
          ObjectId = Schema.ObjectId;
  
      var filingSchema = mongoose.Schema({
          name: String,
          dateFiled: String,
          formName: String,
          XBRLREF: String,
          filingHREF: String,
          title: String,
          type: String
      });
  
      Filing = mongoose.model('Filing', filingSchema);
  
      var companySchema = mongoose.Schema({
          symbol: String,
          name: String,
          secURL: String,
          mc: String,
          filings: [filingSchema],
          irLink: String
      });
  
      Company = mongoose.model('Company', companySchema);
  
      function getCompanies(callback) {
          Company.find({
              cik: {
                  $ne: ""
              }
          }, function(err, companies) {
              if (err) {
                  console.log("error getting companies " + err)
              }
              // console.log(companies.length);
              callback(null, companies);
          })
      };
  
      function createURLs(companies, callback) {
          // create a URL for querying the SEC website for the main, first company page with 100 filings of type 10 only
          companies.forEach(function(company) {
              if (company.symbol) {
                  // console.log("company.nasdaqSymbol is: " + company.nasdaqSymbol);
                  // create url to download
                  cik = company.cik
                  // If you try to run this code you can use 'AMZN' in place of the cik below (most other symbols too)
                  company.link = 'http://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&amp;CIK=' + cik + '&amp;type=10&amp;owner=exclude&amp;count=100&amp;output=xml'
              }
          });
          callback(null, companies);
      }
  
      function downloadSEC(companies, callback) {
          // iterate through each URL and query the SEC website - get the results and return page as data
          companies.forEach(function(company) {
              download(company.link, function(data) {
                  if (data) {
                      // Call separate function that will grab the elements we want from SEC meta-page
                      // Example data/response page: view-source:http://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&amp;CIK=AMZN&amp;type=&amp;dateb=&amp;owner=exclude&amp;count=100&amp;output=xml
                      getLinks(data, company);
                  }
              });
          });
      };
  
      function getLinks(data, company) {
          var $ = cheerio.load(data);
          var filings = [];
          $('filing').each(function() {
              // Have tried many versions of the below including var filing = new Filing etc....  also company.filings.push(filing) (throws: type cast error)
              // Example data: view-source:http://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&amp;CIK=AMZN&amp;type=&amp;dateb=&amp;owner=exclude&amp;count=100&amp;output=xml
              var filing = {
                  dateFiled: $(this).find("dateFiled").text(),
                  xbrlRef: $(this).find('XBRLREF').text(),
                  formName: $(this).find('formName').text(),
                  type: $(this).find('type').text(),
                  filingHREF: $(this).find('filingHREF').text()
              };
              filings.push(filing);
          });
          update(company, filings);
      }
  
      function update(company, filings) {
          console.log('company_id is: ' + company._id);
          // Have tried seemingly thousands of combinations of C[c]ompany.save/update etc. below
          company.save({
              _id: company._id
          }, {
              filings: filings
          }, function(err, company) {
              if (err) {
                  console.log("error updating filings: " + err);
              }
              console.log('company is: ' + company);
          });
      }
  
      async.waterfall([getCompanies, createURLs, downloadSEC, getLinks]);
  </code></pre>