简体   繁体   English

流星包:将JSON数组导入mongoDB

[英]Meteor-package: Import JSON-array to mongoDB

I'm trying to create a meteor-package to import JSON-files to collections in a mongoDB. 我正在尝试创建一个流星包,以将JSON文件导入mongoDB中的集合。 But I'm not quite sure, if this is possible. 但是我不太确定是否可行。

So I want the user to upload a JSON-file. 因此,我希望用户上传JSON文件。 In a input-field the user also types the collection-name which has to be used for the import. 用户还可以在输入字段中输入必须用于导入的收集名称。 After this the JSON-array should be saved to the given collection. 之后,JSON数组应保存到给定的集合。

HTML: HTML:

<template name="importJSON">
    <form id="importJson">
        <input type="text" id="collection">
        <input type="file" id="file">
    </form>
</template>

meteor: 流星:

Template.importJSON.events({
    'submit #importJson': function(e){
        e.preventDefault();
        var collection = e.target.collection.value;
        var obj = JSON.parse(response);
        db.collection.insert(obj);
    }
});

So I have three problems with that: 所以我有三个问题:

1) How do I have to do the upload itself, as the file should be uploaded temporarily 1)我必须自己上载,因为文件应该暂时上载

2) How can I use the collection name given in the input-field? 2)如何使用输入字段中提供的集合名称?

3) How do I import the data in a correct way? 3)如何以正确的方式导入数据? Insert would just append the new data to the existing data, wouldn't it? 插入只会将新数据附加到现有数据上,不是吗?

So to answer your three problems: 因此,回答您的三个问题:

1) How do I have to do the upload itself, as the file should be uploaded temporarily 1)我必须自己上载,因为文件应该暂时上载

If all you need to do is read the file, then insert it into a collection. 如果您只需要阅读文件,则将其插入集合中即可。 In that case you do not need to even upload the file. 在这种情况下,您甚至不需要上传文件。 Just read the file in client side. 只需在客户端读取文件即可。 Here is an article on Reading files in JavaScript using the File APIs . 这是一篇有关使用File API使用JavaScript读取文件的文章。

2) How can I use the collection name given in the input-field? 2)如何使用输入字段中提供的集合名称?

Say the collection name given in the input-field is products and assume you have a sample data file like this: 假设在输入字段中给出的集合名称是products并假设您有一个示例数据文件,如下所示:

{
  "name": "Product",
  "id": "Product identifier",
  "name": "Name of the product",
  "price": "9990",
  "tags": ["tag1", "tag2"]
}

At this point you need to decide how you do this. 此时,您需要确定如何执行此操作。 If you already have a Products collection on server side. 如果服务器端已经有一个Products集合。

<your app name>/lib/collections/products.js

Products = new Meteor.Collection('Products');

Then in your client side code: 然后在您的客户端代码中:

var rawproducts = (content of the file you read using the File API -- as mentioned above)
var newproducts = JSON.parse(rawproducts);

for (var i = 0; i < newproducts.length; i++) {
  Products.insert(newproducts[i]);
}

You can also test this out locally by creating a local only collection. 您还可以通过创建仅本地集合在本地进行测试。

//pass null as collection name, it will create
//local only collection
Products = new Mongo.Collection(null);

for (var i = 0; i < newproducts.length; i++) {
 Products.insert(newproducts[i]);
}

Note: If you use a local only collection your data is still on the client. 注意:如果使用仅本地集合,则数据仍在客户端上。 You'll need to sync that with the server (as described above) for persistence. 您需要将其与服务器同步(如上所述)以实现持久性。

3) How do I import the data in a correct way? 3)如何以正确的方式导入数据? Insert would just append the new data to the existing data, wouldn't it? 插入只会将新数据附加到现有数据上,不是吗?

Importing the data as shown above will keep on appending the data. 如上所示导入数据将继续追加数据。 You may want to think about how to de-dupe the data or completely overwrite the existing data. 您可能需要考虑如何对数据进行重复数据删除或完全覆盖现有数据。

Hope this helps. 希望这可以帮助。


Update: 更新:

How to delete all the elements from a collection? 如何从集合中删除所有元素?

Products.remove({}) // remove every product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM