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, ...

Get Mongoose for Application Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.