0

I'm trying to create a meteor-package to import JSON-files to collections in a mongoDB. But I'm not quite sure, if this is possible.

So I want the user to upload a JSON-file. 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.

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

2) How can I use the collection name given in the input-field?

3) How do I import the data in a correct way? Insert would just append the new data to the existing data, wouldn't it?

0

1 Answer 1

1

So to answer your three problems:

1) How do I have to do the upload itself, as the file should be uploaded temporarily

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.

2) How can I use the collection name given in the input-field?

Say the collection name given in the input-field is products and assume you have a sample data file like this:

{
  "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.

<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? 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
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, thanks! How can I empty the given collection before adding the data?
@user3142695 I have now updated the answer to include the details on how to delete all the elements from a collection?
Thanks. Hopefully the last question: I try to make the complete thing dynamically. That means the user can select the collection, which should be used. So how do I do Products = new Meteor.Collection('Products'); this with a variable? The input field #collection gives the collection - which is in your example 'Products'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.