1

I am trying to upload an Excel file to MongoDB database.

I used below NPM package manager to convert Excel data to MongoDB

mongoXlsx.xlsx2MongoData(path, model, function(err, data){
  console.log(data); // (This variable data has an array of objects, each object is a row in Excel.)
}); 

The image attached has the data. Now, I want to upload this 'data' (That has an array of objects) into MongoDB. How can I do that? Please suggest.enter image description here

2
  • Mongodb only takes data which are in json format, are you making sure that before pushing your data to mongodb, you are converting it into correct json format ? Commented Apr 25, 2018 at 20:33
  • Please format the code. just click on edit, select the code and type ctrl-K Commented Apr 26, 2018 at 1:53

1 Answer 1

1

You can use a for loop to iterate through the array , and insert each element of the array in a single mongoDB document :

for ( var counter=0 ;counter<data.length;counter++)
{
    dataBaseName.colletionName.insertOne(data[counter]);
}

but before that, as @zenwraight pointed out, you should convert your data into correct json format , for example Name : 'ABC' should become 'Name' : 'ABC' (same goes for other fields)

Sign up to request clarification or add additional context in comments.

Comments

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.