MongoDb 表结构和表模型
   1 分钟阅读

mongoose 的表结构和表模型

  • 表结构(/schemas/contents.js)
  • 表模型(/models/Content.js)

一、表结构

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
var mongoose = require(mongoose);
var Content = mongoose.Schema;
//表结构
var contentSchema = new Content({
    title:String,
    category: {
        type:mongoose.Schema.Types.ObjectId,
        ref:'Category'
    },
    description: {
        type:String,
        default:''
    },
    content: {
        type:String,
        default:''
    }
});
module.exports = contentSchema;

二、表模型

1
2
3
4
var mongoose = require(mongoose);
var contentSchema = require(../schemas/contents);//先引用表结构
var Content = mongoose.model(Content, contentSchema);//指定表模型
module.exports = Content;

三、使用

1
2
3
4
5
6
7
8
9
var Content = require(../models/Content)
router.get(/content, function (req, res, next) {
    Content.find().populate(category).then(function (contents) {
        res.render(admin/content_index, {
            userInfo:req.userInfo,
            contents:contents
        });
    })
})
本文目录