This is not a duplicate as the questions I "duplicated" is talking about removing id's from subchemas. Here I want to prevent mongoose from filling the hours in my calendar with id's that reference nothing. It's just supposed to store lessons with lesson id's.
I'm trying to make a calendar with weeks, days and hours, where each hour should be able to reference a Lesson.
This is my calendar schema:
var mongoose = require("mongoose");
var calendarSchema = new mongoose.Schema({
owner: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
weeks: [{
number: Number,
days: [{
number: Number,
hours: [{
number: Number,
available: { type: Boolean, default: false },
booked: { type: Boolean, required: true, default: false },
lesson: {type: mongoose.Schema.Types.ObjectId, ref: "Lesson"}
}]
}]
}]
});
module.exports = mongoose.model("Calendar", calendarSchema);
I create a new calendar for each user when they register using this function
generateCalendar = function() {
var calendar = {};
var date = new Date(2017,0,2);
calendar.weeks = [];
for (var w = 0; w < 52; w++) {
calendar.weeks[w] = {};
calendar.weeks[w].number = w+1;
calendar.weeks[w].days = [];
for (var d = 0; d < 7; d++) {
calendar.weeks[w].days[d] = {};
calendar.weeks[w].days[d].date = new Date(date.setDate(date.getDate()+1))
calendar.weeks[w].days[d].hours = [];
calendar.weeks[w].days[d].number = d+1;
for (var h = 0; h < 24; h++) {
calendar.weeks[w].days[d].hours[h] = {};
calendar.weeks[w].days[d].hours[h].number = h+1;
}
}
}
return calendar;
}
as input when creating the calendar
Calendar.create(generateCalendar(), function(err, calendar) {
if (err) {
console.log(err);
} else {
user.calendar = calendar;
user.save();
calendar.owner = user;
calendar.save();
res.redirect('/dashboard');
}
});
The problem is that each hour in the users calendar now contains a mongoose object id even if I haven't assigned any lesson.
This is what a typical hour looks like:
{ available: true,
booked: false,
_id: 5890b0f1abe4da6e879e2e38,
number: 13 }
I don't know why there's an id in the hour, or what it's referencing! And if I add a Lesson at hour 13 it becomes
{ available: false,
booked: true,
lesson: 5890b1dfabe4da6e879e520a,
_id: 5890b0f1abe4da6e879e2e38,
number: 13 }