xlsx-mongo
v1.0.15
Published
Streamlined Import, Export, and CRUD Operations between XLSX and MongoDB
Downloads
7
Maintainers
Readme
About The Project
Seamless Integration for Importing, Exporting, and Manipulating Data between XLSX and MongoDB.
Getting Started
- Make a mongodb database
- Copy the connection string of the database
- Paste the connection string in the .env file named
MONGO_URL
- Check env_example file for more info - env_example
- Install the package
npm install xlsx-mongo
- Require the package in your main file
const xlsxMongo = require('xlsx-mongo');
Functions
Init function is required to be run before any other function.
xlsxMongo.init(filePath);
Import data from excel file to the specified mongodb collection.
xlsxMongo.import(collectionName, showConsoleMessages);
Export data from the specified mongodb collection to excel file.
const exportFilePath = path.join(__dirname, 'Export.xlsx');
xlsx2mongo.export(collectionName, exportFilePath, showConsoleMessages)
Add data from excel file to the specified mongodb collection.
xlsxMongo.add(collectionName, filePath, showConsoleMessages);
Insert data to the specified mongodb collection.
const insertData = { 'Name': 'Kurizu', 'Address': 'poopy' };
xlsx2mongo.insert(collectionName, insertData, showConsoleMessages)
Delete data from the specified mongodb collection.
xlsxMongo.delete(collectionName, showConsoleMessages);
Update data from the specified mongodb collection.
xlsx2mongo.update(collectionName, updateCriteria, updateData, showConsoleMessages)
Find data from the specified mongodb collection.
xlsx2mongo.find(collectionName, findCriteria, showConsoleMessages)
Find data with projection from the specified mongodb collection.
const findCriteriaPro = { 'Name': 'Kurizu' };
const projection = { 'Name': 1 };
xlsx2mongo.findWithProjection(collectionName, findCriteriaPro, projection, showConsoleMessages)
Replace data with new excel file
const replaceFilePath = path.join(__dirname, 'Replace.xlsx');
xlsx2mongo.replace(collectionName, replaceFilePath, showConsoleMessages)
Check env_example file for more info - env_example
Usage
const xlsx2mongo = require('xlsx-mongo');
const mongoose = require('mongoose');
require('dotenv').config()
const path = require('path');
const filePath = path.join(__dirname, 'Test2.xlsx');
const showConsoleMessages = false;
xlsx2mongo.init(filePath);
const collectionName = 'test';
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(async () => {
// Import data from the Excel file to the specified collection
xlsx2mongo.import(collectionName, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// // Add data from the Excel file to the specified collection
xlsx2mongo.add(collectionName, filePath, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Insert data to the specified collection
const insertData = { 'Name': 'Kurizu', 'Address': 'poopy' };
xlsx2mongo.insert(collectionName, insertData, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Export data from the specified collection to the Excel file
const exportFilePath = path.join(__dirname, 'Export.xlsx');
xlsx2mongo.export(collectionName, exportFilePath, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Delete data from the specified collection
xlsx2mongo.delete(collectionName, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Update data from the specified collection
// to update single row
const updateCriteria = { 'Name': 'efrwdawd' };
const updateData = { $set: { 'Name': 'John Doe' } };
xlsx2mongo.update(collectionName, updateCriteria, updateData, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// to update multiple rows
const updateCriteriaMultiple = { 'Name': 'dwgdrthg', 'Address': 'grgdrgd' };
const updateDataMultiple = { $set: { 'Name': 'Kurizu', 'Address': 'poopy' } };
xlsx2mongo.update(collectionName, updateCriteriaMultiple, updateDataMultiple, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Find data from the specified collection
const findCriteria = { 'Name': 'Kurizu' };
xlsx2mongo.find(collectionName, findCriteria, showConsoleMessages).then((res) => {
console.log(res);
mongoose.connection.close();
});
// Find data with projection
const findCriteriaPro = { 'Name': 'Kurizu' };
const projection = { 'Name': 1 };
xlsx2mongo.findWithProjection(collectionName, findCriteriaPro, projection, showConsoleMessages).then((res) => {
console.log(res);
mongoose.connection.close();
});
// Replace data with new excel file
const replaceFilePath = path.join(__dirname, 'Replace.xlsx');
xlsx2mongo.replace(collectionName, replaceFilePath, showConsoleMessages).then(() => {
mongoose.connection.close();
});
// Avoid running the above functions at the same time
})
.catch((err) => {
console.error('Error:', err);
});
For more information on how to use it visit
If you want me to add more functions or have any issues with the package, feel free to contact me on discord kurizu.taz
or open an issue on github.
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Package Made by: kurizu.taz
on discord
Github - https://github.com/crizmo/xlsx-mongo