0

I have two types of JSON files that I want to insert to my mongodb database. I am using mongoose and nodejs to try that. How can I do it? I tried to look online for possible solutions, but I found nothing. Both of the JSON files have around hundred of inputs. So, inserting one by one not a good solution. I will give the sample of the data here for schema.

Type 1:

[
 { 
   "name" : "Jonathan",
   "lastName": "Smith"
 },
 { 
   "name" : "Simon",
   "lastName": "Williams"
 }
]

Type 2:

[
 { 
   "id": 1,
   "type": "fruit",
   "inStock": [
     {
       "count": 15,
       "items": [
          {
            "name": "apple",
            "amount": 12
          },
          {
            "name": "orange",
            "amount": 3
          }
       ]
     }  
},
{ 
   "id": 2,
   "type": "vegetable",
   "inStock": [
     {
       "count": 18,
       "items": [
          {
            "name": "carrot",
            "amount": 10
          },
          {
            "name": "lettuce",
            "amount": 8
          }
       ]
     }  
 }
]

What would my schema be like? How do I parse my JSON file? An example code would be appreciated.

Ok, here is the code I wrote for this.

const fs = require("fs");
const mongoose = require('mongoose');
var assert = require('assert')
mongoose.connect('mongodb://localhost:27017/person'), { useNewUrlParser: true};


var personSchema = new mongoose.Schema({
    name: String,
    lastname: String
});

const Person = mongoose.model("Person", personSchema);

var data = fs.readFileSync('people.json');
let mydata = JSON.parse(data);

console.log(mydata);

Person.collection.insertMany(mydata, function(err) {
    if(err){
        console.log("There was an error");
        console.log(err);
    }else{
        console.log("Success");
    }
    
 })

Console gives me Success. I have a db. But, I do not have a collection. What am I doing wrong here?

I think my problem is not that my code is not working. It is for some reason my code, even if it is a simple tutorial code will not save any collection to database. I do not know why.

  • possible duplicate of https://stackoverflow.com/questions/30696946/how-to-import-json-into-mongodb-using-mongoose – CYBERDEVILZ Oct 15 '21 at 17:20
  • I do not think you can use the same code with JSON file that has hundreds of data. – foreverStudent Oct 15 '21 at 17:31
  • The above mentioned method is used to import an array of json documents. Hence it will work for a single or multiple json documents containing hundreds of data. Just give it a try. It has to work – CYBERDEVILZ Oct 15 '21 at 17:35
  • It seems my code was a success. But, whenever I try to do show dbs, it shows 0.000GB on collection – foreverStudent Oct 15 '21 at 17:58
  • I added my code to my question. Any response would be appreciated. – foreverStudent Oct 15 '21 at 18:16
  • take a look at the example in the [docs](https://mongoosejs.com/docs/api.html#model_Model.insertMany). I don't think you need `.collection` before `insertMany` – Joe Oct 16 '21 at 07:20

0 Answers0