Home > Blockchain >  How do I create a number of objects with insertMany with existing data from the db?
How do I create a number of objects with insertMany with existing data from the db?

Time:03-12

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.

CodePudding user response:

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

  • Related