//文章分类
articlecate
- id
- title
- description
- add_time
article user
- id -id
- cid -username
- title -password
- description -name
- author_id -age
- author_name -sex
- content -tel
- add_item -add_time
article的cid和articlecate的id关联
article的author_id和user的id关联
//user.js
const mongoose = require('./db');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username:{type:String,unique:true},
password:String,
name:String,
age:Number,
sex:String,
tel:Number,
status:{
type:Number,
status:1
}
})
module.exports = mongoose.model('User',UserSchema,'user');
//article.js
const mongoose = require('./db');
const Schema = mongoose.Schema;
const ArticleSchema = new Schema({
title:{type:String,unique:true},
cid:{type:Schema.Types.ObjectId},/* 文章分类 */
author_name:{
type: String
},
author_id:{
type:Schema.Types.ObjectId /* 用户的id */
},
description:String,
content:String
})
module.exports = mongoose.model('Article',ArticleSchema,'article');
//articlecate.js 文章分类
const mongoose = require('./db');
const Schema = mongoose.Schema;
const ArticlecateSchema = new Schema({
title:{type:String,unique:true},
description:String,
add_tiem:{
type:Date
}
})
module.exports = mongoose.model('Articlecate',ArticlecateSchema,'articlecate');
/* 查询文章的信息 并显示文章的分类 作者的信息 */
var data = ArticleModel.aggregate([
{$lookup:{
from:"articlecate",
localField:'cid',
foreignField:'_id',
as:"details"
}},
{
$lookup:{
from:"user",
localField:'author_id',
foreignField:'_id',
as:'userInfo'
}
}
])