Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda