mongoose的表结构和表模型

表结构(/schemas/contents.js)

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;

表模型(/models/Content.js)

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

使用:

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
});
})
})

具体用法需要看官网API

未经允许不得转载:朱邦邦的博客 » mongoose的表结构和表模型

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址