1. //文章分类
    2. articlecate
    3. - id
    4. - title
    5. - description
    6. - add_time
    1. article user
    2. - id -id
    3. - cid -username
    4. - title -password
    5. - description -name
    6. - author_id -age
    7. - author_name -sex
    8. - content -tel
    9. - add_item -add_time

    article的cid和articlecate的id关联
    article的author_id和user的id关联

    1. //user.js
    2. const mongoose = require('./db');
    3. const Schema = mongoose.Schema;
    4. const UserSchema = new Schema({
    5. username:{type:String,unique:true},
    6. password:String,
    7. name:String,
    8. age:Number,
    9. sex:String,
    10. tel:Number,
    11. status:{
    12. type:Number,
    13. status:1
    14. }
    15. })
    16. module.exports = mongoose.model('User',UserSchema,'user');
    1. //article.js
    2. const mongoose = require('./db');
    3. const Schema = mongoose.Schema;
    4. const ArticleSchema = new Schema({
    5. title:{type:String,unique:true},
    6. cid:{type:Schema.Types.ObjectId},/* 文章分类 */
    7. author_name:{
    8. type: String
    9. },
    10. author_id:{
    11. type:Schema.Types.ObjectId /* 用户的id */
    12. },
    13. description:String,
    14. content:String
    15. })
    16. module.exports = mongoose.model('Article',ArticleSchema,'article');
    1. //articlecate.js 文章分类
    2. const mongoose = require('./db');
    3. const Schema = mongoose.Schema;
    4. const ArticlecateSchema = new Schema({
    5. title:{type:String,unique:true},
    6. description:String,
    7. add_tiem:{
    8. type:Date
    9. }
    10. })
    11. module.exports = mongoose.model('Articlecate',ArticlecateSchema,'articlecate');
    1. /* 查询文章的信息 并显示文章的分类 作者的信息 */
    2. var data = ArticleModel.aggregate([
    3. {$lookup:{
    4. from:"articlecate",
    5. localField:'cid',
    6. foreignField:'_id',
    7. as:"details"
    8. }},
    9. {
    10. $lookup:{
    11. from:"user",
    12. localField:'author_id',
    13. foreignField:'_id',
    14. as:'userInfo'
    15. }
    16. }
    17. ])