MongoDB - $lookup 没有得到适当的结果

问题描述 投票:0回答:1

这里我只需要查找三个表,首先需要根据classid和boardid从主题表中获取所有主题,然后从内容表中我们需要获取所有主题和内容详细信息并按topicid对它们进行分组(请查看预期输出)。那么每个主题详细信息应该包含我们将从 edchildrevisioncompleteschemas 表中获取的子详细信息 #查找代码

const { stageid, subjectid, boardid, scholarshipid, childid } = req.params;
edcontentmaster
.aggregate([
  {
    $match: {
      stageid: stageid,
      subjectid: subjectid,
      boardid: boardid,
      // scholarshipid: scholarshipid,
    },
  },
  {
    $addFields: {
      convertedField: {
        $cond: {
          if: { $eq: ["$slcontent", ""] },
          then: "$slcontent",
          else: { $toInt: "$slcontent" },
        },
      },
    },
  },
  {
    $sort: {
      slcontent: 1,
    },
  },
  {
    $lookup: {
      from: "edchildrevisioncompleteschemas",
      let: { childid: childid, subjectid:subjectid,topicid:"$topicid" },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$childid",
                    "$$childid"
                  ]
                },
                {
                  $in: [
                    "$$subjectid",
                    "$subjectDetails.subjectid"
                  ]
                },
                {
                  $in: [
                    "$$topicid",
                    {
                      $reduce: {
                        input: "$subjectDetails",
                        initialValue: [],
                        in: {
                          $concatArrays: [
                            "$$value",
                            "$$this.topicDetails.topicid"
                          ]
                        }
                      }
                    }
                  ]
                }
              ]
            }
          }
        },
        {
          $project: {
            _id: 1,
            childid: 1
          }
        }
      ],
      as: "studenttopic",
    },
  },
  {
    $group: {
      _id: "$topic",
      topicimage: { $first: "$topicimage" },
      topicid: { $first: "$topicid" },
      sltopic: { $first: "$sltopic" },
      studenttopic: { $first: "$studenttopic" },
      reviewquestionsets: {
        $push: {
          id: "$_id",
          sub: "$sub",
          topic: "$topic",
          contentset: "$contentset",
          stage: "$stage",
          timeDuration: "$timeDuration",
          contentid: "$contentid",
          studentdata: "$studentdata",
          subjectIamge: "$subjectIamge",
          topicImage: "$topicImage",
          contentImage: "$contentImage",
          isPremium: "$isPremium",
        },
      },
    },
  },
  {
    $project: {
      _id: 0,
      topic: "$_id",
      topicimage: 1,
      topicid: 1,
      sltopic: 1,
      studenttopic:1,
      contentid: "$contentid",
      reviewquestionsets: 1,
    },
  },
])
.sort({ sltopic: 1 })
.collation({
  locale: "en_US",
  numericOrdering: true,
})

从上面的查询中,我获得了单个主题的适当数据,但我需要主题表中的所有主题,并且每个主题应该具有与我为单个主题获得的相同格式数据, 前 mongoplayground.net/p/LoxSBI3jZL-

电流输出-

[
{
"reviewquestionsets": [
  {
    "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey 
from RigerLesson - 1",
    "contentset": "Lesson - 1",
    "id": ObjectId("64ccd53792362c7639d3da5f"),
    "stage": "STD-5",
    "timeDuration": "15",
    "topic": "The Monkey from Riger"
  },
  {
    "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey 
 from RigerLesson - 3",
    "contentset": "Lesson - 3",
    "id": ObjectId("64ccf5ca92362c7639d3f145"),
    "isPremium": true,
    "stage": "STD-5",
    "timeDuration": "5",
    "topic": "The Monkey from Riger"
  }
],
"sltopic": "1",
"studenttopic": [
  {
    "_id": ObjectId("659580293aaddf7594689d18"),
    "childid": "WELL1703316202984"
  }
],
"topic": "The Monkey from Riger",
"topicid": "1691144002706",
"topicimage": ""
}
]

预期产出-

[
{
"_id": "64cc9a2656738e9f1507f521",
"subjectid": "1691130406151",
"subject": "English",
"subjectImage": "https://wkresources.s3.ap-south- 
1.amazonaws.com/1691761437925_644750345.png",
"stageid": "5",
"stage": "STD-5",
"boardid": "1",
"boardname": "BSE",
"scholarshipid": "NVOOKADA1690811843420",
"scholarshipname": "Adarsh",
"createon": "2023-08-04T06:26:46.154Z",
"updatedon": "2023-08-14T13:07:16.256Z",
"__v": 0,
"slsubject": "1",
"topicDetails": {
  "reviewquestionsets": [
    {
      "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey 
from RigerLesson - 1",
      "contentset": "Lesson - 1",
      "id": "64ccd53792362c7639d3da5f",
      "stage": "STD-5",
      "timeDuration": "15",
      "topic": "The Monkey from Riger"
    },
    {
      "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey 
from RigerLesson - 3",
      "contentset": "Lesson - 3",
      "id": "64ccf5ca92362c7639d3f145",
      "isPremium": true,
      "stage": "STD-5",
      "timeDuration": "5",
      "topic": "The Monkey from Riger"
    }
  ],
  "sltopic": "1",
  "studenttopic": [
    {
      "_id": "659580293aaddf7594689d18",
      "childid": "WELL1703316202984"
    }
  ],
  "topic": "The Monkey from Riger",
  "topicid": "1691144002706",
  "topicimage": ""
}
}
]
node.js mongodb mongoose mongodb-query aggregation-framework
1个回答
0
投票

您应该通过 $lookupedcontentmaster 一起加入

subject
集合。
pipeline
阶段中的
$lookup
应该是您现有的查询。

在最后阶段,通过获取第一个元素将

topicDetails
转换为对象。

db.subject.aggregate([
  {
    $match: {
      stageid: "5",
      subjectid: "1691130406151",
      boardid: "1",
      scholarshipid: "NVOOKADA1690811843420"
    }
  },
  {
    $lookup: {
      from: "edcontentmaster",
      let: {
        stageid: "$stageid",
        subjectid: "$subjectid",
        boardid: "$boardid",
        scholarshipid: "$scholarshipid"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$stageid",
                    "$$stageid"
                  ]
                },
                {
                  $eq: [
                    "$subjectid",
                    "$$subjectid"
                  ]
                },
                {
                  $eq: [
                    "$boardid",
                    "$$boardid"
                  ]
                },
                {
                  $eq: [
                    "$scholarshipid",
                    "$$scholarshipid"
                  ]
                }
              ]
            }
          }
        },
        {
          $addFields: {
            convertedField: {
              $cond: {
                if: {
                  $eq: [
                    "$slcontent",
                    ""
                  ]
                },
                then: "$slcontent",
                else: {
                  $toInt: "$slcontent"
                }
              }
            }
          }
        },
        {
          $sort: {
            slcontent: 1
          }
        },
        {
          $group: {
            _id: "$topic",
            topicimage: {
              $first: "$topicimage"
            },
            topicid: {
              $first: "$topicid"
            },
            sltopic: {
              $first: "$sltopic"
            },
            studenttopic: {
              $first: "$studenttopic"
            },
            reviewquestionsets: {
              $push: {
                id: "$_id",
                sub: "$sub",
                topic: "$topic",
                contentset: "$contentset",
                stage: "$stage",
                timeDuration: "$timeDuration",
                contentid: "$contentid",
                studentdata: "$studentdata",
                subjectIamge: "$subjectIamge",
                topicImage: "$topicImage",
                contentImage: "$contentImage",
                isPremium: "$isPremium"
              }
            }
          }
        },
        {
          $lookup: {
            from: "edchildrevisioncompleteschemas",
            let: {
              childid: "WELL1703316202984",
              //childid,
              subjectid: "1691130406151",
              //subjectid,
              topicid: "$topicid"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [
                      {
                        $eq: [
                          "$childid",
                          "$$childid"
                        ]
                      },
                      {
                        $in: [
                          "$$subjectid",
                          "$subjectDetails.subjectid"
                        ]
                      },
                      {
                        $in: [
                          "$$topicid",
                          {
                            $reduce: {
                              input: "$subjectDetails",
                              initialValue: [],
                              in: {
                                $concatArrays: [
                                  "$$value",
                                  "$$this.topicDetails.topicid"
                                ]
                              }
                            }
                          }
                        ]
                      }
                    ]
                  }
                }
              },
              {
                $project: {
                  _id: 1,
                  childid: 1
                }
              }
            ],
            as: "studenttopic"
          }
        },
        {
          $project: {
            _id: 0,
            topic: "$_id",
            topicimage: 1,
            topicid: 1,
            sltopic: 1,
            studenttopic: 1,
            contentid: "$contentid",
            reviewquestionsets: 1
          }
        }
      ],
      as: "topicDetails"
    }
  },
  {
    $set: {
      topicDetails: {
        $first: "$topicDetails"
      }
    }
  }
])

演示@Mongo Playground

© www.soinside.com 2019 - 2024. All rights reserved.