In order to build a simple article publishing system, all articles need to belong to one category. Ask for advice on how to design the database. One level is enough. The novice has just learned the database. .
//article model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var AticleSchema = new Schema({
title: { type: String },
author_id: { type: String},
content:{type:String},
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
});
AticleSchema.index({title: 1}, {unique: true});
mongoose.model('Aticle', AticleSchema);
The simplest and cruelest way is to establish two schema, one article and one article classification through table association.
1. schema of classified documentsvar mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; //This is the relevant place var AticleCategorySchema = new Schema({ Name: String, // sort name Aticle: [{type: objectid, ref:' aticle'}]//table association })
2. The schema of another article document is the same as that written by yourself, with only a few modifications.
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; //This is the relevant place var AticleSchema = new Schema({ title: { type: String }, author_id: { type: String}, content:{type:String}, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now }, AticleCategory:{ // association table type:ObjectId, ref:'AticleCategory' }, });
The rest of the code is added in your own way.