Sunday, 7 May 2017

Mongoose insertMany with a possible lookup on a populate

I have a MEAN application and I am allowing users to upload a CSV containing multiple Event rows. After praising the CSV I have a list of Event objects and I can insert the records using mongoose insertMany. I have an Event and Attendee schema like this (simplified for this question).

var EventSchema = new mongoose.Schema({
    name: String,
    date: Date,
    location: String,
    speaker: { type: ObjectId, ref: 'Attendee' },
});

var AttendeeSchema = new mongoose.Schema({
    firstname: String,
    email: {type: String, unique : true},
    speaker: Boolean,
});

My issue is when users are uploading the CSV they will only know the email address of the speaker but not the ObjectId. This email will always be unique. While performing an Event.insertMany() is there a way to do a Attendee.findOne on the email and populate the speaker field with an ObjectId ref to an Attendee on each Event as they are inserted.



via Sage Hornung

No comments:

Post a Comment