Subdocuments
Subdocuments are very similar to the ordinary documents we have been using so far. They are individual documents with their own schema. The big difference is that subdocuments are documents that are stored within a parent document, instead of a MongoDB collection of their own.
Perhaps an example will demonstrate this best. In our MongoosePM application, tasks are currently lacking in functionality as tasks
for a given project is just a string. It would be better if a task had a distinct schema like the following:
var taskSchema = new mongoose.Schema({ taskName: { type: String, required: true, validate: validateLength }, taskDesc: String, createdOn: { type: Date, default: Date.now }, createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, modifiedOn: Date, assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } });
This schema looks a bit more useful right? In a relational database this would be a standalone table, and you would run...