Embedded Documents in Mongoose

Quick tip: Notes on using embedded models in Mongoose

Recently I was working on a project where I needed to use embedded documents in Mongoose.
When I first tried this, this is what my code looked like.

var Part = require('./Part'); 
var Build = require('./Build'); 

var Build = new Schema({
	parts: [Part]

This didn't work, and I didn't know why. I kept getting a Cast Error on the parts property.

Accessing Schemas vs Accessing Models

After messing around for a bit, I stumbled across this article on Stack Overflow and I realized that in my code I
was accessing the model, not the schema.

If you want to embed a document in another document of a mongoose model, then you need to access the schema.

To do this, you need to make the change as follows:

var Part = required('./Part').schema; 
var Build = require('./Build'); 

var Build = new Schema({
	parts: [Part]

And that should work for you!

But what if I want to use Populate?

populate is the method that Mongoose gives you to create relational data. It's powerful if you use it right, and it can give your data great depth.


Subscribe to Dylan Lott

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.