【问题标题】:My express mongoose populate not giving the result of other collection我的快递猫鼬填充没有给出其他集合的结果
【发布时间】:2019-01-31 00:08:55
【问题描述】:

我在猫鼬中创建了两个集合,如下所示 1.类别架构 const categorySchema = new Schema({

                    name:{
                        type:String,
                        required:true
                    },
                    cat_type:{
                        type:String,
                        enum:['images','quotes','videos']
                    },
                    image:{
                        type:String
                    },
                    videoId:{
                        type:Number
                    }
    })


      const Category = mongoose.model('categories',categorySchema); 
      module.exports = Category;

2.视频架构

        const videoSchema = new Schema({
                    videoId:{
                        type:String
                    },
                    title:{
                        type:String,
                        required:true
                    },
                    cat_id:{
                            type:Schema.Types.ObjectId,
                            ref:'categories'
                    },
                    description:{
                        type:String
                    },
                    url:{
                        type:String
                    },
                    publishedAt:{
                        type:String
                    }

           })

        const Video = mongoose.model('videos',videoSchema);
        module.exports = Video   

我正在使用以下查询来获取视频和视频的类别详细信息

          Video.find({}).
          populate('categories')
          .exec(function(err, data){
          console.log(data);
          })

但我只是得到视频表的结果,而不是类别表,我在这做错了什么?

【问题讨论】:

    标签: node.js express mongoose


    【解决方案1】:

    您要填充字段 cat_id,而不是类别。您放入填充的字段应该是引用其他模式的字段,在本例中为 cat_id

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 2020-11-19
      • 2015-08-26
      • 2021-01-25
      • 2019-08-29
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多