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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.