Importing an Excel file into MongoDB

MongoDB does not support the direct import of Excel files, so to do that, we will use a function built into Excel.

Getting ready

The mongoimport utility supports only JSON, CSV, and TSV files. Therefore, to get your data from Excel into MongoDB, the best option is to save it as a CSV file, and then use mongoimport to import it.

How to do it…

In Excel:

  1. Go to the File menu.
  2. Select Save As.
  3. Save the file in the Comma Separated Values (CSV) format.

After you perform the preceding steps, you can use the previous recipe to import the file.

If you think that's too easy though, you can import the Excel file into a Pandas DataFrame using read_excel, write the entire DataFrame to a CSV file using to_csv, and then import it using mongoimport. I highly recommend the first and much easier option.

How it works…

This recipe works almost exactly like our previous recipe for importing a CSV file; only here, we start with an Excel file, which we save as a CSV file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset