Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 更新嵌入式文档Mongoose-现有实例保存/更新等_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 更新嵌入式文档Mongoose-现有实例保存/更新等

Node.js 更新嵌入式文档Mongoose-现有实例保存/更新等,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,在我的一生中,我似乎无法将嵌入文档的数组更新/添加到父文档中 请原谅冗长的脚本,但如果我没有包含所有的代码,它将不会帮助我,也不可能帮助我。请随时提供任何其他节点建议 我已经尝试了数百种变体,并查看了所有可能的资源,以使其工作,或做类似的事情,但已经失败了几个小时了。我将非常感谢任何帮助使这项工作。如果我错过了什么,请告诉我 这个答案:是最接近于帮助我的(因为我已经有了公司/实例),但唉,我还没有让它工作 谢谢你的帮助 Node --version v0.10.21 mongod

在我的一生中,我似乎无法将嵌入文档的数组更新/添加到父文档中

请原谅冗长的脚本,但如果我没有包含所有的代码,它将不会帮助我,也不可能帮助我。请随时提供任何其他节点建议

我已经尝试了数百种变体,并查看了所有可能的资源,以使其工作,或做类似的事情,但已经失败了几个小时了。我将非常感谢任何帮助使这项工作。如果我错过了什么,请告诉我

这个答案:是最接近于帮助我的(因为我已经有了公司/实例),但唉,我还没有让它工作

谢谢你的帮助

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]);

我已经把你的代码中没有包含的部分(数据库和下载)拼凑在一起,我想我的一切都正常工作了

我正在使用mongoose 3.8和node 0.10,但我认为它仍然适用于您。 你可以在这里做几件事,但这是一个问题


我还注意到async.瀑布不需要getLinks函数,因为您没有使用回调。

注意:问题只存在于底部的model.save/update部分。我已经成功地解决了类似问题。你能告诉我你到底在哪里面对这个问题吗?问题是,如果我需要的话,将文件实例化,并将其添加到母公司。因此,从本质上讲,如何将文件传递给Mongoose。我认为类似于:
company.fileings.push(files)
然后
company.update(_id:company.id,company,function(err,company){…
本来可以工作,但是会抛出一个错误。谢谢Jwags。我还有另一种工作方式——使用save。谢谢你花时间回复。我还遇到了一些导入错误,我认为这阻碍了我的成功。
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);
    });
}