将数据上传到Meteor / Mongo DB [英] Upload Data to Meteor / Mongo DB

查看:255
本文介绍了将数据上传到Meteor / Mongo DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Meteor应用程式,想要将资料(来自csv)上传到一个流星集合。

I have a Meteor app and would like to upload data (from csv) to a meteor collection.

我找到了:

  • solutions (e.g. Collectionfs) which deal with file uploads
  • methods for uploading directly to the underlying mongo db from the shell
  • references to meteor router - but I am using the excellent iron-router, which does not appear to provide this functionality

我的要求是应用程序用户能够将csv数据上传应用程序内的应用程序。我不需要将csv文件存储在应用程序文件结构中的任何位置,我只需要将csv数据读入集合即可。

My requirement is that the app user be able to upload csv data to the app from within the app. I do not need to store the csv file anywhere within the app file structure, I just need to read the csv data to the collection.

这是可能的,我不能弄清楚如何做到这一点,因为我的职权范围('上传数据到流星')是不明确或不正确的。

It is possible that I cannot figure out how to do this because my terms of reference ('upload data to meteor') are ambiguous or incorrect. Or that I am an idiot.

推荐答案

我已经使用我的这个要点,以及此代码(使用 jquery-csv 插件来解析csv数据)。这是在客户端完成的,并且独立于使用铁路由器或不是。将插入代码移动到Meteor方法,首先上传csv文件,然后解析并将数据插入服务器是相当简单的。我也尝试过,但没有看到任何性能改进。

I've solved this problem in the past using this gist of mine, together with this code (using the jquery-csv plugin to parse the csv data). This is done on the client side and is independent of using iron-router or not. It would be fairly straightforward to move the insertion code into a Meteor method, uploading the csv file first and then parsing and inserting the data on the server. I've tried that, too, but didn't see any performance improvement.

$(document).ready(function() {
    var dd = new dragAndDrop({
        onComplete: function(files) {
            for (var i = 0; i < files.length; i++) {
                // Only process csv files.
                if (!f.type.match('text/csv')) {
                    continue;
                }
                var reader = new FileReader();
                reader.onloadend = function(event) {
                    var all = $.csv.toObjects(event.target.result);
                    // do something with file content
                    _.each(all, function(entry) { 
                         Items.insert(entry);
                    });
                }
             }
        }
     });

     dd.add('upload-div'); // add to an existing div, turning it into a drop container
});

请注意,如果你插入了很多条目,关闭一段时间,直到所有的插入。否则,服务器和浏览器选项卡上的节点将变得非常慢。请参阅我的建议解决方案: Meteor的订阅和同步缓慢

Beware though that if you are inserting a lot of entries, then you are better off turning all reactive rerendering off for a while, until all of them are inserted. Otherwise, both node on the server and the browser tab will get really slow. See my suggested solution here: Meteor's subscription and sync are slow

这篇关于将数据上传到Meteor / Mongo DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆