Mongodb import data from excel

You can handle loading the Excel file content by writing Java code using Apache POI library [//poi.apache.org/]. The library is developed for working with MS office application data including Excel.

I have recently created the application based on the technology that will help you to load Excel files to the MongoDB database.

The application is available under //www.abespalov.com/ and tested only for Windows, but should work for Linux as well. The application will create necessary collection automatically and populate the collection with the Excel file content. You can export several files in parallel. You can skip the step to convert the files into the CSV format. The application handles the xls and xlsx formats.

Overall application stages are :

1] Load the excel file content. Here is the code depending on file extension:

fileExtension = FilenameUtils.getExtension[inputSheetFile.getName[]];

if [fileExtension.equalsIgnoreCase["xlsx"]] {
        workbook = createWorkbook[openOPCPackage[inputSheetFile]];
} else {
        workbook = createWorkbook[openNPOIFSFileSystemPackage[inputSheetFile]];
}

sheet = workbook.getSheetAt[0];

2] Establish the MongoDB connection. I use the MongoClientURI library;

MongoClientURI mongoClientURI = new MongoClientURI[
                    "mongodb://" + dbUser + ":" + dbPassword + "@" + dbServer 
+ ":" + dbPort + "/" + dbDatabase];
            excel2db.mongoClient = new MongoClient[mongoClientURI];

3] Iterate over the sheet and inset rows into the collection. Here is a piece of Java code :

Row row = [Row] rowIterator.next[];

    //get column names from a header
    short minColIdx = row.getFirstCellNum[];
    short maxColIdx = row.getLastCellNum[];

    ArrayList columnNameList = new ArrayList[];
    String columnName;

    logger.info["The table {} is being populated", tableName];

    //populate a list of column names
    for [short colIdx = minColIdx; colIdx < maxColIdx; colIdx = [short] [colIdx + 1]] {
        columnNameList.add[row.getCell[colIdx] == null? "": row.getCell[colIdx].toString[]];
    }

    while [rowIterator.hasNext[]] {

        Document document = new Document[];
        Row rowData = [Row] rowIterator.next[];

        numOfProcessedRows++;
        for [short colIdx = minColIdx; colIdx < maxColIdx; colIdx = [short] [colIdx + 1]] {
            document.put[columnNameList.get[colIdx], rowData.getCell[colIdx].toString[]];
        }

        //save the document into a collection, point to the database
        MongoCollection mongoCollection = mongoDB.getCollection[tableName];
        mongoCollection.insertOne[document];

    }
}    

Here you can find all Java code for the application created for exporting excel to Postgres [//github.com/palych-piter/Excel2DB].

This article explains how to transfer data from Excel to MongoDB using the Excel Add-In for MongoDB.

The CData Excel Add-In for MongoDB enables you to edit and save MongoDB data directly from Excel. This article explains how to transfer data from Excel to MongoDB. This technique is useful if you want to work on MongoDB data in Excel and update changes, or if you have a whole spreadsheet you want to import into MongoDB. In this example, you will use the restaurants table; however, the same process will work for any table that can be retrieved by the CData Excel Add-In.

Establish a Connection

If you have not already done so, create a new MongoDB connection by clicking From MongoDB on the ribbon.

Set the Server, Database, User, and Password connection properties to connect to MongoDB. To access MongoDB collections as tables you can use automatic schema discovery or write your own schema definitions. Schemas are defined in .rsd files, which have a simple format. You can also execute free-form queries that are not tied to the schema.

Retrieve Data from MongoDB

To insert data into MongoDB, you will first need to retrieve data from the MongoDB table you want to add to. This links the Excel spreadsheet to the MongoDB table selected: After you retrieve data, any changes you make to the data are highlighted in red.

  1. Click the From MongoDB button on the CData ribbon. The Data Selection wizard is displayed.
  2. In the Table or View menu, select the restaurants table.
  3. In the Maximum Rows menu, select the number of rows you want to retrieve. If you want to insert rows, you need to retrieve only one row. The Query box will then display the SQL query that corresponds to your request.
  4. In the Sheet Name box, enter the name for the sheet that will be populated. By default the add-in will create a new sheet with the name of the table.

Insert Rows to MongoDB

After retrieving data, you can add data from an existing spreadsheet in Excel.

  1. In a cell after the last row, enter a formula referencing the corresponding cell from the other spreadsheet; for example, =MyrestaurantsSheetInExcel!A1.
  2. After using a formula to reference the cells you want to add to MongoDB, select the cells that you are inserting data into and drag the formula down as far as needed. The referenced values you want to add will be displayed on the restaurants sheet.
  3. Highlight the rows you want to insert and click the Insert Rows button.

As each row is inserted, the Id value will appear in the Id column and the row's text will change to black, indicating that the record has been inserted.

How do I import a CSV file into MongoDB?

If you have CSV files [or TSV files - they're conceptually the same] to import, use the --type=csv or --type=tsv option to tell mongoimport what format to expect. Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.

How do I import files into MongoDB?

To import JSON file you need to follow the following steps: Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don't close this cmd to stay connected to the server. Step 2: Open another command prompt and run the mongo shell. Using the mongo command.

How do I import an Excel file into node?

How to Import Excel File Records in MySQL Database via Node Js using Multer.
Step 1: Create Project Folder..
Step 2: Set Up Package JSON..
Step 2: Install NPM Packages..
Step 3: Generate MySQL Table..
Step 4: Create Bootstrap Upload Form..
Step 5: Make Server File..
Step 6: Upload Excel to MySQL..

How do I import a collection in MongoDB?

To import data to a MongoDB database, you can use mongoimport to import specific collections data, or you can use mongorestore to import a binary [BSON] full database backup. The exported database file must be stored locally on the same machine as your client.

Chủ Đề