【问题标题】:how to put data from json file in mongoDB using nodejs如何使用nodejs将json文件中的数据放入mongoDB
【发布时间】:2021-11-11 03:09:12
【问题描述】:

我的 json 文件中有这样的数据 -

[{
        "end_year": "",
        "intensity": 6,
        "sector": "Energy",
        "topic": "gas",
        "insight": "Annual Energy Outlook",
        "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
        "region": "Northern America",
        "start_year": "",
        "impact": "",
        "added": "January, 20 2017 03:51:25",
        "published": "January, 09 2017 00:00:00",
        "country": "United States of America",
        "relevance": 2,
        "pestle": "Industries",
        "source": "EIA",
        "title": "U.S. natural gas consumption is expected to increase during much of the projection period.",
        "likelihood": 3
    }]

我想使用 nodeJS 将它插入到我的 mongoDB 中。

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

首先,非常好的问题。基本上,我们可以通过多种方式将 JSON 文件导入 MongoDB。

如果您的 JSON 文件结构遵循您想要导入的数据库架构或特定集合架构

1.第一种方式

您可以使用 MongoDB Compass 来import the JSON into MongoDB Database 如果您没有 MongoDB Compass,您可以轻松download from here

2。第二种方式

您可以使用 MongoDB Compass 的替代品,例如 Robo 3T, 您可以使用 Robo 3T - Follow this step-by-step way 轻松将 JSON 导入 Mongodb。

以上两种方式都不错,自己喜欢用哪一种。

其次,如果您有一个未根据您的数据库架构或特定架构组织的 JSON 文件,那么您需要根据您的数据库架构组织您的 JSON 文件,然后将其导入数据库。这是一种非常简单的方法。

按照这条路:

  1. Fs from nodejs
  2. Migrate mongoose

逻辑是,在您的项目中使用 npm 或 yarn 安装 migrate mongoose 包。按照文档进行配置。那么你只需要在你的迁移文件中使用 fs 读取 JSON 文件并构建 JSON 文件并将查询插入到数据库里面迁移猫鼬的功能。

您将轻松解决此问题

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2016-08-31
    • 2019-07-10
    • 2014-12-24
    • 1970-01-01
    • 2017-10-21
    • 2019-07-02
    • 2014-03-01
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多