我似乎无法在我的生命中更新/添加嵌入文档数组到父文档。
请原谅冗长的剧本,但如果我没有包含所有代码,它就不会帮助我或者可以提供帮助。随意提供任何其他Node建议。
我已经尝试了数百种变体,并且已经查看了所有可能的资源以使其工作,或者做类似的事情,但现在几个小时都没有成功。我非常感谢任何帮助这项工作。如果我错过了什么,请告诉我。
这个答案:How to update embedded document in mongoose?最接近帮助我(因为我已经拥有公司/实例)但是唉,我还没有让它发挥作用。
感谢您的帮助!
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&CIK=' + cik + '&type=10&owner=exclude&count=100&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&CIK=AMZN&type=&dateb=&owner=exclude&count=100&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&CIK=AMZN&type=&dateb=&owner=exclude&count=100&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]);
答案 0 :(得分:1)
我已经将未包含的代码部分(数据库和下载)整合在一起,我认为我已经完成了所有工作。
我使用的是mongoose 3.8和节点0.10,但我认为它仍然适合你。 你可以在这里做几件事,但这会发出mongodb findAndModify
function update(company, filings) {
console.log('company_id is: ' + company._id);
Company.findByIdAndUpdate(company._id, {
filings: filings
}, function(err, company) {
if (err) {
console.log("error updating filings: " + err);
}
console.log('company is: ' + company);
});
}
我还注意到async.waterfall不需要getLinks函数,因为你没有使用回调。