Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda