node.js - 如何以编程方式将 excel/CSV 数据导入 MongoDB 集合/文档(MERN)
问题描述
我有一个包含员工数据的 Excel 表。我的任务是将excel文件中的数据存储在MongoDB数据库>员工集合中(mongodb文档中excel表中的一行)。我在一个反应应用程序中做这一切。我想到了使用 mongoimport。由于我需要 CSV 或 Json 格式的文件,因此我使用 SheetJs npm 包将我的 excel 文件转换为 CSV,并创建了一个 csv 类型的 blob 文件。然后使用下面的命令,我能够将该 CSV 文件导入我的 mongoDB 数据库。
mongoimport --db demo --collection employees --type csv --headerline --file /path/to/myfile.csv
但是我通过提供本地磁盘的路径从 mongo shell 做到了这一点。现在我正试图在我的反应应用程序中实现这一点。最初我是按照这个想法进行的——只要我上传了一个 excel 文件,我就会将它转换为 CSV 文件,然后我会在正文中调用一个带有该 CSV 文件的 post api。发送该请求后,我将在我的 nodejs 后端/服务器中调用“mongoimport”命令,以便该 CSV 文件中的数据将存储在我的 mongoDb 集合中。现在我无法以编程方式找到 usmongoimport 命令的任何解决方案。如何在我的 nodejs 服务器代码中调用“Mongoimport”命令?我找不到任何有关它的文档。
如果这不是完成这项任务的正确方法,请向我建议任何完全其他方式来完成这项任务。用外行的话来说,我想使用 Reactjs/nodejs 应用程序将数据从 excel 文件导入 MongoDb 数据库。
解决方案
how are you? First of all, mongoimport also allows you to import TSV files (same command as for CSV but putting --type tsv), many times friendlier to use with Excel.
Regarding mongoImport, I regret to report that mongoImport cannot be used by any means other than the command line.
What you can do from NodeJs is execute commands in the same way that they are executed by a terminal. For this you can use the child_proccess Module or Exec Function. I hope that helps even a little. Regards!
推荐阅读
- angular - 一次性获取响应式表单值
- c++ - 在 yaml-cpp 中流式传输我自己的类型的正确方法是什么?
- pycharm - Jekyll 前端问题在 JetBrains IDE 中标记为错误
- python - Anaconda Navigator 不会启动笔记本
- php - 迭代数组并返回最大值 php
- html - 如何将 CSS 添加到“不是第一个”呈现的样式化组件
- android - Android 测试用例 robolectric Shadowlooper idle VS idleFor
- ruby-on-rails - Ruby on rails pg_search multisearch 附加属性?
- c++ - MIX AUDIO:添加音频缓冲区
- geolocation - 让 Apple Wallet 中的卡片根据位置执行请求