Reputation: 117
If I have model Products:
var ProductSchema = new Schema({
title: {
type: String,
maxlength: 20,
required: true
},
description: {
type: String,
maxlength: 300
},
price: Number,
active: Boolean,
category: {
}
});
And I must create category, type ObjectID with reference to category model I create right now this:
var CategorySchema = new Schema({
name: {
type: String,
maxlength: 300
},
description: {
type: String,
maxlength: 300
}
});
Can someone what to do now? Because i do not know how.
Upvotes: 0
Views: 1118
Reputation: 496
The category object in the schema should be as following:
category: {
type: Schema.Types.ObjectId,
ref: 'Category' //category model name
}
For more information about references you should read the following paragraph:
DBRefs are references from one document to another using the value of the first document’s _id field, collection name, and, optionally, its database name. By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection. To resolve DBRefs, your application must perform additional queries to return the referenced documents like population queries.
Upvotes: 3