想弄个简单的文章发布系统,所有文章需要属于一个分类,求教数据库怎么设计,一级就够。新手刚学数据库这块。。
//文章模型
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);
给我你的怀抱2017-04-28 09:05:43
The simplest and crudest way is to create two schemas, one for articles and one for article classification, through table association.
1. Schema of classified documents
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId; // 这个是关联的地方
var AticleCategorySchema = new Schema({
name: String, // 分类名字
aticle:[{type:ObjectId,ref:'Aticle'}] // 表关联
})
2. The schema of another article document is the same as the one you wrote yourself, with just a few modifications
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId; // 这个是关联的地方
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:{ // 关联表
type:ObjectId,
ref:'AticleCategory'
},
});
Add the rest of the code your way.