Skip to main content

Mongoose Sub-Documents

Lesson Objectives#

  1. Create a schema for properties of models that are objects

Create a schema for properties of models that are objects#

Properties of models can be objects and arrays (see below)

const articleSchema = new Schema({  title: { type: String, required: true, unique: true },  author: { type: String, required: true },  body: String,  comments: [{ body: String, date: Date }], // can have arrays of objects with specific properties  date: { type: Date, default: Date.now },  hidden: Boolean,  meta: {    // can have properties that are objects    votes: Number,    favs: Number,  },});

If we want to, we can use schemas instead of explicitly typing (or retyping) the structure of this object

const authorSchema = new Schema({  name: { type: String },  articles: [articleSchema],});

This can be then be used normally to push articles onto an author's articles property

//Create constructor functionsconst Article = mongoose.model("Article", articleSchema);const Author = mongoose.model("Author", authorSchema);
//instantiate an author and an articleconst matt = new Author({ name: "Matt" });const article1 = new Article({ title: "Awesome Title", author: matt.name }); //Note that
//push the article onto matt's articles arraymatt.articles.push(article1);
//save bothmatt.save();article1.save();

We can also find a sub document by id

console.log(matt.articles.id(article1.id));

update it

matt.articles.id(article1.id).title = "altered title";matt.save(); //saving the parent saves the child, BUT NOT THE ORIGINAL SAVED IN THE ARTICLES COLLECTION

and delete it

matt.articles.id(article1.id).remove();matt.save(); //saving the parent will remove the child from the parent, BUT WILL NOT REMOVE THE ORIGINAL SAVED IN THE ARTICLES COLLECTION

We can create sub docs on the fly too, but it won't automatically add it to the parent's array. We need to do that manually

const subdoc_article = matt.articles.create({  title: "Create via Matt article property",  author: matt.name,});matt.articles.push(subdoc_article);
matt.save(); //save an article to save the addition to its sub docsArticle.create(subdoc_article); //create the article in the collection

ALWAYS BE AWARE!! UPDATING A SUB DOCUMENT WILL NOT MODIFY THE ORIGINAL IF IT IS SAVED IN ANOTHER COLLECTION. YOU MUST ALSO UPDATE ANY DUPLICATES**#