Node.js应用中将Excel转换的对象数组存入MongoDB的方法
Got it, let's walk through how to get that array of objects from your Excel file into MongoDB. Since you're already using mongoXlsx, I'll cover both Mongoose (which plays nicely with the model parameter you're already using) and the native MongoDB driver, so you can pick what fits your setup.
You're already passing a model to xlsx2MongoData—I assume this is a Mongoose Schema or Model, which makes this approach super straightforward:
First, ensure your MongoDB connection is set up
You should already have code like this somewhere in your project to connect to your database:const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/your-database-name') .then(() => console.log('Connected to MongoDB successfully')) .catch(err => console.error('MongoDB connection error:', err));Batch insert the parsed data in the callback
Thedatavariable fromxlsx2MongoDatais exactly the array of objects Mongoose needs. Use theinsertManymethod on your Model to push everything into the database:const mongoXlsx = require('mongo-xlsx'); const YourModel = require('./path-to-your-mongoose-model'); // Import your defined Model const excelFilePath = './your-excel-file.xlsx'; const schemaModel = YourModel.schema; // Use your existing Schema for mapping mongoXlsx.xlsx2MongoData(excelFilePath, schemaModel, function(err, data){ if (err) { console.error('Failed to parse Excel file:', err); return; } // Insert the entire array into MongoDB YourModel.insertMany(data) .then(insertedDocs => { console.log(`Successfully added ${insertedDocs.length} rows to the database!`); }) .catch(dbError => { console.error('Error inserting data into MongoDB:', dbError); }); });- Pro tip: If you want to allow partial inserts (even if some rows fail validation), add
{ ordered: false }as a parameter toinsertMany.
- Pro tip: If you want to allow partial inserts (even if some rows fail validation), add
If you're not using Mongoose and prefer the official native driver, here's how to do it:
- Set up the client and connect to the database
const { MongoClient } = require('mongodb'); const mongoXlsx = require('mongo-xlsx'); const dbUri = 'mongodb://localhost:27017'; const client = new MongoClient(dbUri); const databaseName = 'your-database-name'; const collectionName = 'your-target-collection'; async function loadExcelToMongo() { try { // Connect to the database await client.connect(); const db = client.db(databaseName); const collection = db.collection(collectionName); // Parse the Excel file (using promise wrapper for async/await) const excelData = await new Promise((resolve, reject) => { mongoXlsx.xlsx2MongoData('./your-excel-file.xlsx', null, (err, parsedData) => { if (err) reject(err); else resolve(parsedData); }); }); // Batch insert the parsed data const insertResult = await collection.insertMany(excelData); console.log(`Inserted ${insertResult.insertedCount} documents successfully`); } catch (error) { console.error('Process failed:', error); } finally { // Always close the client connection await client.close(); } } // Run the function loadExcelToMongo();
- Data validation: Make sure your Excel data matches the expected schema/field types for your MongoDB collection. Mongoose will automatically validate against your Schema, but with the native driver you'll need to handle validation yourself.
- Large files: If your Excel file is huge, parsing all data at once might use too much memory. Consider using a streaming Excel parser instead of
mongoXlsxfor very large datasets. - Duplicate data: To avoid inserting duplicates, you can pre-check existing documents or use
updateManywithupsert: trueto either insert new rows or update existing ones.
内容的提问来源于stack exchange,提问作者Anirudh

