how to import excel file (XLSX) to mongoDB

2020-05-17 06:43发布

I have a set of data as input to be given to MongoDB in XLSX format. How am I supposed to import the Excel file as input to MongoDB?

Is there any plugin available to import xlsx files as input to MongoDB?

7条回答
啃猪蹄的小仙女
2楼-- · 2020-05-17 07:12

There is a pymongo extention utilities package, one of the modules there does exactly this, imports an excel file to a mongo collection or a complete excel workbook to a mongo database. You can find documentation and examples here:
and you can install the library with `pip install mongoUtils

  • you will need to also install xldr library
  • disclaimer: I am the author of this library
查看更多
该账号已被封号
3楼-- · 2020-05-17 07:14

You can upload data of multiple sheets from one excel into mongodb at once using this code.
Your first row i.e "0" row will be consider as column title and rest data of that column.

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
           DB db = mongoClient.getDB("yourdb_name");
           System.out.println("Connected to Database successfully");
           DBCollection coll = db.getCollection("your_collection name");
           System.out.println("Collection your_collection name selected successfully");

                DBCollection OR = db.getCollection("Input_Container");
                System.out.println("Collection Device_Details selected successfully");
                OR.drop();
                DBObject arg1 = null;
                //coll.update(query, update);
                DBCollection OR_UPLOAD = 
                db.createCollection("Input_Container", arg1);
                String path =" your file path";

                File myFile = new File(path);
                FileInputStream inputStream = new FileInputStream(myFile);
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                int number=workbook.getNumberOfSheets();
                  System.out.println("NumberOfSheets "+number);

                  for(int i=0;i<number;i++)
                  {
                XSSFSheet sheet = workbook.getSheetAt(i);
                int col_value=sheet.getRow(0).getLastCellNum();
                int row_num= sheet.getLastRowNum();
                System.out.println("row_num "+row_num);
                List<String> DBheader = new ArrayList<String>();
                List<String> Data = new ArrayList<String>();

                for(int z=1;z<=row_num;z++){
                     DBheader.clear();
                     Data.clear();
                 for(int j=0;j<col_value;j++)
                {
                    if(sheet.getRow(0).getCell(j).toString()!=null || sheet.getRow(0)!=null)
                    {
                    String cel_value = sheet.getRow(0).getCell(j).toString();
                    DBheader.add(cel_value.trim());
                    }
                    else{
                        break;

                    }
                }
                for(int k=0;k<col_value;k++){
                    String data =" ";   
                    if(sheet.getRow(z).getCell(k)!=null)
                    {
                    data =  sheet.getRow(z).getCell(k).toString();
                    }
                    Data.add(data.trim());

                    }
                BasicDBObject doc = new BasicDBObject();
                System.out.println("Data.size() "+Data.size());

                int l=0;
                for(String headers:DBheader)
                { 
                if(l>Data.size()){break;}
                    doc.append(headers, Data.get(l));
                    l++;
                 }
                OR_UPLOAD.insert(doc);
                }

            }System.out.println("File Upload Done");
                  mongoClient.close();
查看更多
Bombasti
4楼-- · 2020-05-17 07:30

mongoimport -d admin -c Mongocsv --type csv --file Mongocsv.csv --headerline

connected to: 127.0.0.1 imported 5 objects

mongo

use admin switched to db admin

db.Mongocsv.find()

查看更多
冷血范
5楼-- · 2020-05-17 07:30

You can handle loading the Excel file content by writing Java code using Apache POI library (https://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 http://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<String> 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 (https://github.com/palych-piter/Excel2DB).

查看更多
对你真心纯属浪费
6楼-- · 2020-05-17 07:35

You cannot import an XLSX file into MongoDB directly. However, what you can do with an Excel spreadsheet is save it as a CSV file, then use mongoimport to import it into MongoDB. You can find the documentation for mongoimport here, but in any case, the command you need to run should look something like the following:

mongoimport --db myDb --collection myCollection --type csv --headerline --file /path/to/myfile.csv

In the command above, the --headerline flag indicates that the first line in your file contains the name of the fields. There are many other options you can use depending on your needs. These are highlighted in the documentation.

查看更多
贼婆χ
7楼-- · 2020-05-17 07:35

I used "fast-csv" to upload csv onto mongoDB database.

Sample Code:

var csv = require("fast-csv");

csv.fromPath('./test.csv',{headers: true})
    .on("data", function(data){
       var details = new Details;
       details=data;
       details.save(function (saveErr, savedetail) {
             if (saveErr) {
                   console.log(saveErr)
             }
        });
    })
    .on("end", function(){
        console.log("done");
    })
查看更多
登录 后发表回答