7

I have a question about nesting Mongoose schema.

Here is a simple code snippet

var aSchema = new Schema({bar: String});
var bSchema = new Schema({a: aSchema, foo: String});
var cSchema = new Schema({as: [aSchema], foo:String});

This will throw TypeError on bSchema: TypeError: Undefined type at 's' Did you try nesting Schemas? You can only nest using refs or arrays., but works fine for cSchema.

Just want to ask why bSchema does not work. Cannot find explanation in Mongoose doc. Thanks.

1
  • Is the a property of bSchema supposed to be a reference to an aSchema object? Don't do that. Do: a:{type: Mongoose.Schema.ObjectId', ref: 'a'} where the a in ref:a is the name of the mongoose model that you create using aSchema Commented Dec 2, 2014 at 21:44

2 Answers 2

8

MongoDB is not a relational database. This can cause confusion for some who are used to the RDBS model (I still get tripped up occasionally...but I'm really not a DB guy).

Oftentimes, you'll find it beneficial to reference other documents in your Mongo entities. Mongoose schemas provide a very simple and effective way to do this that feels very relational.

When defining a schema that will store a reference to a different type of document, you define the relevant property as an object with a type and a ref. Typically when defining schema properties, you can simply say: a: Number; however, Mongoose provides many different options for a schema property other than type:

a: {
   type: Number,
   required: true   
}

Setting required: true will prevent us from saving a document where the a property is not present.

Once you understand how to define your schemas with object definitions, you can leverage Mongoose's population mechanic:

a: {
   type: Mongoose.Schema.ObjectId,
   ref: 'a'
}

This tells Mongoose to store the ObjectId (a Mongoose-specific identifier) of a specific a document as the a property of our schema. Still following me?

When setting this property on a Mongoose document, you can simply say: doc.a = myA. When you go to save doc, Mongoose will automagically make the conversion and only store the ID in your database.

When retrieving a document that references another schema, you'll need to populate. I won't go into that, but its pretty simple - check out the documentation.

2

I was facing this issue as I was completely new to the MongoDB. Later I found we need to use the relationship as below with the help of Mongoose.

Below is my country schema.

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var CountrySchema = new Schema({
    name: { type: String, required: true },
    activeStatus: Boolean,
    createdOn: Date,
    updatedOn: Date
});

And I can use this schema in my State schema as follows.

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var StateSchema = new Schema({
    name: { type: String, required: true },
    country: {type: ObjectId, ref: "Country"},
    activeStatus: Boolean,
    createdOn: Date,
    updatedOn: Date
});

Here I am using pointing to my other schema with the help of ref.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.