Mongoose之多表查詢語法

const mongoose=require('mongoose');

let contentSchema=mongoose.Schema;

module.exports=new contentSchema({
    category:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'categories'
    },
    title:String,
    desc:String,
    content:String
});

ref的值是categories的模板

router.get('/content',function(req,res){
    Contents.find().populate('category').then(function(contents){
        console.log('~~~~~~~~~~~~~~~~~~contents');
        console.log(contents);
        res.render('admin/content_index',{
            userInfo:req.userInfo,
            contents:contents
        });
    })

})

調用聯合查詢需要使用populate方法。。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章