mongoose关联表结构

 

var articleSchema = new mongoose.Schema({
  
    comments: {

    },
    // 关联字段==把分类的_id存在这
    category: {
        type: mongoose.Schema.Types.ObjectId,
        // 引用  ref:后的是Classify模型
        ref: "Classify" 

    },



})
//创建文章的时候给option的value绑定 分类的id
<select name="category" id="category" v-model="article.classify"  >
            <option  v-for="(item,index) in classifyList"  v-bind:value='item._id'>{{item.name}}</option>
        </select>
// 发布文章的时候将分类的_id存进去
router.post('/article/create', (req, res) => {
  var article = req.body
  console.log(article)
  new Article({
    title: article.title,
    content: article.content,
    category:article.classify
  }).save().then(rs=>{
      res.send('200')
  })

})
// 获取所有文章
//populate内是文章属性,这样一来category就保存了分类下的所有属性 ,前端想要调用就category.name,如果没有populate这一步存的仅仅是_id
router.get('/article/getlist',(req,res)=>{
    Article.find().populate('category').then(doc=>{
        console.log(doc)
        res.send(doc)
    })
})

 

转载于:https://www.cnblogs.com/wuyushuo/p/7424370.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值