These are chat archives for Automattic/mongoose

1st
Feb 2017
Minerva Rubalcava
@marubalcava
Feb 01 2017 03:41
Hello o/ How's everyone?. I want some help.
I'm new in mongo/mongoose/NoSQL Database paradigm and I have a question about the architecture of my database. I'm using mongoose and I have two collections Courses and Users, a course can have many Users and an User can be enrolled in more than one Course, say a ManyToMany relationship. I set property in both Schemas pointing to each other, but I want to store the Enrollment date also, so I was thinking on create a third collection for this. Is this a correct approach or there is a way that I don't know to do it? (I'm more familiar to RBDs).
Jake Geiser
@Geiser9
Feb 01 2017 04:05
@marubalcava you shouldnt need a separate collection for enrollment date. I would personally just pass it through to the specific course database along with all the user info when they enroll in the class. you can pass through the enrollment date like this
var someSchema = new mongoose.Schema({
    user: String,
    enrollmentDate: {type: Date, default: Date.now}
});
pretty new to this myself so take it with a grain of salt but for my blog whenever a new one is created the date is automatically included with it
Minerva Rubalcava
@marubalcava
Feb 01 2017 04:14
Thanks @Geiser9, but a Course can have many users, every user with a enrollment date as they enroll, and have different enrollment dates for every course they enroll as well. This is my definition.
let courseSchema = new Schema({
  title:       String,
  book:        { type:Schema.Types.ObjectId, ref:'Book' },
  user:        { type:Schema.Types.ObjectId, ref:'User' },//who created the course/who the course belongs to
  description: String,
  startDate:   Date,
  endDate:     Date,
  createdAt:   { type:Date, default:Date.now },
  updateddAt:  { type:Date, default:Date.now },
})
let userSchema = new Schema({
  name:       String,
  username:   String,
  email:      String,
  role:       String,
  createdAt:  { type:Date, default:Date.now },
  updateddAt: { type:Date, default:Date.now },
})
ups, this code doesn't get formatted :/
yeah! o/
Minerva Rubalcava
@marubalcava
Feb 01 2017 04:23
Or you meant something like this in the courseSchema?
 enrolled: [{
         date: Date,
         user: {
             type: Schema.Types.ObjectId,
             ref: 'User'
         }
  }]