使用聚合错误在子嵌套数组中填充同一级别的演员和导演:不填充它们

问题描述

我想使用猫鼬聚合功能填充所有演员和导演。这是我的模型。

             const tvShowSchema = new Schema({

               name: String,year: Number,country: String,seasons: [{

                          number: Number,episodes:[{
                                     title: String,number: Number,releasedOn: Date,description: String,cast:[
                                  {type: Schema.Types.ObjectId,ref:'actors'
                                      }],director:{
                                      type: Schema.Types.ObjectId,ref:'directors'
                                       }

                                        },{timeStamps: true
                                    }

                                      ]

                                 }]

                             });

我使用查找作为函数来填充演员和导演。这是我在电视节目控制器中的方法:

                  export const getTvShowEpisode = async(req,res) =>{

                                    try{

                             const { tvShowId } = req.params;

                             const { seasonId } = req.params;

                             const { episodeId } = req.params;

                            const tvShowEpisode = await TVShow.aggregate([



                               {"$unwind": "$seasons"},{"$unwind": "$seasons.episodes"},{"$match":{

                                 "_id": Types.ObjectId(tvShowId),"seasons._id": Types.ObjectId(seasonId),"seasons.episodes._id": Types.ObjectId(episodeId)

                                       }},//WHY don't populate actors and director

                               {"$lookup": {"from":'actors',"localField": 'seasons.episodes.cast',"foreignField": '_id',"as": 'actors'}
                               },{"$lookup": {"from":'directors',"localField": 
                  'seasons.episodes.director',"as": 'director'}},{"$project": {
                                  "_id": Types.ObjectId(tvShowId),"seasonsNumber": "$seasons.number","seasonsYear": "$seasons.year","episode": "$seasons.episodes"}},]);


                              if (tvShowEpisode) {
                                  res.send(tvShowEpisode);
                                   } else {
                                          res
                                       .status(404)
                             .send({ mensaje: `tv show episode not found` });
                               }
                      }
                            catch(error){
                               console.log(error);
                                res.status(500).send({message: `error: \n\n${error}`});
                                    }

                                 };

我不知道我设置管道操作员的顺序是否错误。我正在使用项目管道来选择我想要的字段。我应该看到特定节目电视剧集的演员和导演的所有信息,但我只看 ID。

我如何才能观看特定电视节目中包含的所有演员和导演,而不仅仅是观看 ID?

这是实际输出:

             [
                     {
                         "seasons": {
                          "_id": "6007609a9cc33a148436060d"
                             },"_id": "60075d219cc33a14843605fe","seasonsNumber": 1,"seasonsYear": 2010,"episode": {
                                     "cast": [
                                        "6004b348b1cd7f28c8d5569d","6004b4323265a91b18f96d39","6004b4423265a91b18f96d3a"
                                            ],"_id": "6007609a9cc33a1484360610","title": "resilence","number": 3,"releasedOn": "2009-08-19T19:00:00.000Z","description": "learning from negative aspects","director": "600759119cc33a14843605fc"
                                 }
                                  }
                                  ]

解决方法

我刚刚解决了。

我不得不从 $project 管道中删除代码行并工作。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...