-1

i have data like this in my json file -

[{
        "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
    }]

and i want to insert it in my mongoDB using nodeJS.

1
  • Duplicate questions are not permitted on Stack Overflow. Please research your inquiry before posting here in the future. Duplicate of NodeJS: saving JSON to MongoDB Commented Sep 16, 2021 at 6:16

1 Answer 1

0

First of all, very nice question. Basically, there are many ways we can import the JSON file into MongoDB.

If you have your JSON file structure followed your database schema or your specific collection schema that you want to import

1. First Way

You can use MongoDB Compass to import the JSON into MongoDB Database If you don't have MongoDB Compass you can easily download from here

2. Second Way

You can use the alternative of MongoDB Compass, Like Robo 3T, You can easily import JSON into Mongodb by using Robo 3T - Follow this step-by-step way.

Both above mention ways are good, It is your choice which one you really like to use.

Secondly, If you have a JSON file with non organize according to your database schema or specific schema then you need to organize your JSON file accordingly to your database schema then import it into the database. It is a very simple way to do it.

Follow this way:

  1. Fs from nodejs
  2. Migrate mongoose

Logic will be, install the migrate mongoose package using npm or yarn in your project. configure it by following the documents. then you just have to read the JSON file using fs in your migrate file and structure the JSON file and make insert the query into the database inside up function of migrating mongoose.

you will fix this issue easily

Hopefully that helps you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.