mongodb 怎么建立文章分类
我想大声告诉你
我想大声告诉你 2017-04-28 09:03:43
0
2
688

想弄个简单的文章发布系统,所有文章需要属于一个分类,求教数据库怎么设计,一级就够。新手刚学数据库这块。。

//文章模型

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);
我想大声告诉你
我想大声告诉你

reply all(2)
给我你的怀抱

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.

Ty80

How to create multiple categories and categories? How to do it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template