'How to predefine MongoDB with initial values before adding more values programmatically?

I am trying to update my MongoDB with initial values before it adds any other values. For example, in my create-react-app, I have a backend file that predefines what the variable people will hold in my current file via a module.exports method from index.cjs:

module.exports = [
  { 
    "id": 1,
    "name": "Arto Hellas", 
    "number": "040-123456"
  },
  { 
    "id": 2,
    "name": "Ada Lovelace", 
    "number": "39-44-5323523"
  },
  { 
    "id": 3,
    "name": "Dan Abramov", 
    "number": "12-43-234345"
  },
  { 
    "id": 4,
    "name": "Mary Poppendieck", 
    "number": "39-23-6423122"
  }
]

My mongo.cjs file that holds the exported information via people variable:

const people = require('./index.cjs')

const mongoose = require('mongoose')

const personSchema = new mongoose.Schema({
    id: Number,
    name: String,
    number: String
})

const password = process.argv[2]

const url = 
    `mongodb+srv://UvZoomE:${password}@cluster0.iiymy.mongodb.net/phonebook?retryWrites=true&w=majority`

mongoose.connect(url)

const Person = mongoose.model('Person', personSchema)

if (process.argv.length <= 3) {
    Person.updateOne(people)
    Person.find({}).then(result => {
        result.forEach(person => {
          console.log(person)
        })
        mongoose.connection.close()
        process.exit(1)
      })
}

const person = new Person({
    id: Math.floor(Math.random() * 100 + 1),
    name: process.argv[3],
    number: process.argv[4]
})

person.save().then(res => {
    console.log(`added ${process.argv[3]} ${process.argv[4]} to phonebook`)
    mongoose.connection.close()
})

I am trying to find out if there is a model method, via mongoose methods, that will allow me to update my database with people before it adds a new value via

    console.log(`added ${process.argv[3]} ${process.argv[4]} to phonebook`)
    mongoose.connection.close()
})

I tried the update method in my if statement, but I am not sure if that is the method I am looking for...



Solution 1:[1]

I found the solution to my problem, the model method I am looking for is insertMany which allows me to insert any predefined information into my Mongo database!

Person.insertMany(people)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SupraCoder