NodeJS/Mongoose schema array of ObjectId
----------------------------------------------
Category Schema
const mongoose = require('mongoose');
const timestamps = require('mongoose-timestamp');
const cateorySchema = new mongoose.Schema({
category_name: {
type: String,
trim: true,
required: true,
},
active: {
type: Boolean,
default: true,
}
});
cateorySchema.plugin(timestamps); // automatically adds createdAt and updatedAt timestamps
module.exports = mongoose.model('Category',cateorySchema);
SubCategory Schema
'use strict'
const mongoose = require('mongoose');
const timestamps = require('mongoose-timestamp');
const subCategorySchema = new mongoose.Schema({
categories:{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
subcategorytitle:{
type:String,
trim:true,
required: true
},
active: {
type: Boolean,
default: true
}
});
subCategorySchema.plugin(timestamps); // automatically adds createdAt and updatedAt timestamps
module.exports = mongoose.model('Subcategory',subCategorySchema);
Categories: Java Script Tags: #NodeJs,