Home  >  Q&A  >  body text

mongodb 怎么建立文章分类

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

//文章模型

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

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-28 09:05:43

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

    reply
    0
  • Cancelreply