【问题标题】:popuate in array of object in mongoose在猫鼬中填充对象数组
【发布时间】:2021-08-16 00:52:58
【问题描述】:

我想从 tvshow 模式中填充 myshow 模式中的 show_detail 吗? 我不知道如何填充对象数组,特别是对象中的元素

const mongoose = require('mongoose');
const Tvshow = require('./tvshows');
const Schema = mongoose.Schema;

const MyshowScheme = new Schema({
    username : {
        type : String
    },
    shows : [
        {
            show_detail : {
                type : Schema.Types.ObjectId,
                ref : 'Tvshow'
            },
            showid : {
                type : Number,
            },
            info : {
                season : {
                    type : Number,
                },
                episode : {
                    type : Number,
                }
            }
        }
    ]

})

module.exports = mongoose.model('Myshow',MyshowScheme);

【问题讨论】:

    标签: javascript mongodb mongoose schema populate


    【解决方案1】:

    很简单:

    Myshow.find({}).populate('shows.show_detail')
    

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 2023-03-16
      • 1970-01-01
      • 2020-11-24
      • 2020-06-28
      • 2020-01-30
      • 2019-11-27
      • 2015-09-21
      相关资源
      最近更新 更多