Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!