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

155
Visualizações
How do I create a number of objects with insertMany with existing data from the db?

I'm currently trying to insert a large number of models through insertMany, but I can't seem to figure out how to populate the array when creating an object. I'm relatively new to Mongoose and any help would be appreciated, here is the code I have right now.

const ProgramsSchema = new mongoose.Schema({
   program_id: {
       type: String,
       required: true
   },
   description: {
       type: String
   },
});
const schoolsSchema = new mongoose.Schema({
    inst_url: {
        type: String
    },
    programs: {
        type: [{type: ProgramsSchema, ref: "Programs"}]
    }
});

And here's the code where I try to create a number of schools and add it to the database.

let new_schools = []
for (let i = 0; i < schools.length; i++) {
  let school = schools[i]
  let p_arr = []
  for (let p_index = 0; p_index < school["PROGRAMS"].length; p_index++) {
     let p_id = school["PROGRAMS"][p_index]
      Programs.find({program_id: p_id}).populate('Programs').exec(function(err, data) {
      if (err) {
        console.log(err);
      } else {
        p_arr.push(data[0])
      }
    })
  }
  let newSchool = {
    inst_url: school["INSTURL"],
    programs: p_arr,
  }
  new_schools.push(newSchool);
}

Schools.insertMany(new_schools);

I can basically add all of the school data into the db, but none of the programs are being populated. I was wondering if there was a way to do this and what the best practice was. Please let me know if you guys need more info or if my question wasn't clear.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There are a few problems with your mongoose schemas. The operation you are trying to do in find is not available, based on your mongoose schemas. You cannot populate from "Programs" to "Schools". You can populate from "Schools" to "Programs", for instance:

Schools.find().populate(programs)

And to do that, several changes in your schemas are necessary. The idea is to store the programs _id in your programs array in School collection and be able to get the programs info through populate(), either regular populate or 'custom populate' (populate virtuals).

Regular populate()

I would change the schoolsSchema in order to store an array of _id into programs:

const schoolsSchema = new mongoose.Schema({
    inst_url: {
        type: String
    },
    programs: [
        {type: String, ref: "Programs"}
    ]
});

You should change ProgramsSchema as well:

const ProgramsSchema = new mongoose.Schema({
   _id: Schema.Types.ObjectId, // that's important
   description: {
       type: String
   },
});

And now, you can do:

Programs.find({_id: p_id}).exec(function(err, data) {
    if (err) {
        console.log(err);
    } else {
        p_arr.push(data[0]._id)
    }
})

Your documents should be inserted correctly. And now you can populate programs when you are performing a query over School, as I indicated above:

Schools.find().populate(programs)

Populate Virtual

The another way. First of all, I have never tried this way, but I think it works as follows:

If you want to populate over fields that are not ObjectId, you can use populate virtuals (https://mongoosejs.com/docs/populate.html#populate-virtuals). In that case, your schemas should be:

const ProgramsSchema = new mongoose.Schema({
   program_id: String,
   description: {
       type: String
   },
});
const schoolsSchema = new mongoose.Schema({
    inst_url: {
        type: String
    },
    programs: [
        {type: String, ref: "Programs"}
    ]
});

Enable virtual in your School schema:

Schools.virtual('programs', {
  ref: 'Programs',
  localField: 'programs',
  foreignField: 'program_id'
});

Then, you should store the program_id.

Programs.find({program_id: p_id}).exec(function(err, data) {
    if (err) {
        console.log(err);
    } else {
        p_arr.push(data[0].program_id)
    }
})

And as before, you can populate() when you need.

I hope I helped

over 4 years ago · Santiago Trujillo 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