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

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

我是 MongoDB 新手,并尝试根据不同集合中子项的

subjectid
completestatus
获取所有主题。

$lookup
之后我没有得到适当的结果。从主集合中根据
subject
获取所有主题详细信息,然后我想使用此表查找该特定孩子是否完成该主题,但我的
[]
字段为空
studenttopic

db = {
  "edchildrevisioncompleteschemas": [
    {
      "_id": "659580293aaddf7594689d18",
      "childid": "WELL1703316202984",
      "subjectDetails": [
        {
          "subjectid": "1691130406151",
          "subject": "English",
          "completestatus": "false",
          "topicDetails": [
            {
              "topicid": "1691144002706",
              "topic": "The Monkey from Riger",
              "completestatus": "true"
            },
            {
              "topicid": "1691154462501",
              "topic": "The Little Round Bun (1)",
              "completestatus": "true"
            }
          ]
        },
        {
          "subjectid": "1691133599736",
          "subject": "ସାମାଜିକ ବିଜ୍ଞାନ",
          "completestatus": "false",
          "topicDetails": [
            {
              "topicid": "1691561384319",
              "topic": "ସାମାଜିକ ଶୃଙ୍ଖଳା",
              "completestatus": "true"
            }
          ]
        }
      ]
    },
    {
      "_id": "659580293aaddf7594689d18",
      "childid": "WELL170331620298978",
      "subjectDetails": [
        {
          "subjectid": "1691130406151",
          "subject": "English",
          "completestatus": "false",
          "topicDetails": [
            {
              "topicid": "1691144002706",
              "topic": "The Monkey from Riger",
              "completestatus": "true"
            }
          ]
        }
      ]
    }
  ],
  "edcontentmaster": [
    {
      "_id": {
        "$oid": "64ccd53792362c7639d3da5f"
      },
      "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey from RigerLesson - 1",
      "boardid": "1",
      "boardname": "BSE",
      "scholarshipid": "NVOOKADA1690811843420",
      "scholarshipname": "Adarsh",
      "stageid": "5",
      "subjectid": "1691130406151",
      "stage": "STD-5",
      "subject": "English",
      "subjectimage": "https://notevook.s3.ap-south-1.amazonaws.com/Adarsh/sub+english.png",
      "topicimage": "",
      "contentimage": "",
      "topicid": "1691144002706",
      "topic": "The Monkey from Riger",
      "slcontent": "1",
      "sltopic": "1",
      "contentset": "Lesson - 1",
      "timeDuration": "15",
      "quiz": [],
      "slsubject": "1"
    },
    {
      "_id": {
        "$oid": "64ccf5ca92362c7639d3f145"
      },
      "contentid": "NVOOKADA1690811843420STD-5EnglishThe Monkey from RigerLesson - 3",
      "boardid": "1",
      "boardname": "BSE",
      "scholarshipid": "NVOOKADA1690811843420",
      "scholarshipname": "Adarsh",
      "stageid": "5",
      "subjectid": "1691130406151",
      "stage": "STD-5",
      "subject": "English",
      "subjectimage": "https://notevook.s3.ap-south-1.amazonaws.com/Adarsh/sub+english.png",
      "topicimage": "",
      "contentimage": "",
      "topicid": "1691144002706",
      "topic": "The Monkey from Riger",
      "slcontent": "3",
      "sltopic": "1",
      "contentset": "Lesson - 3",
      "timeDuration": "5",
      "quiz": [],
      "isPremium": true,
      "videos": [],
      "concepts": [],
      "slsubject": "2"
    }
  ]
}

$lookup
代码:

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,
    },
  },
  {
    $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: childid, subjectid:subjectid,topicid:"$topicid" },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                { $eq: ["$childid", "$$childid"] },
                { $eq: ["$subjectDetails.subjectid", "$$subjectid"] },
              { $eq: ["$subjectDetails.topicDetails.topicid", "$$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,
    },
  },
])

在一个主题下,可以进行多个测试,因此在单个

topicid
中,我们可能会找到一个或多个文档,这就是为什么我将它们按
topicid
进行分组。

node.js database mongodb mongoose aggregation-framework
1个回答
1
投票

你的第二个和第三个条件不起作用:

  1. subjectid
    变量与作为数组的
    subjectDetails.subjectid
    字段进行比较。

  2. topicid
    变量与字段
    subjectDetails.topicDetails.topicid
    (嵌套数组)进行比较。

因此,每个问题的解决方案:

  1. 使用

    $in
    运算符检查
    subjectid
    数组中的
    subjectDetails.subjectid
    变量。

  2. 使用

    $in
    运算符检查展平
    topicid
    数组中的
    subjectDetails.topicDetails.topicid
    变量。

{
  $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"
                        ]
                      }
                    }
                  }
                ]
              }
            ]
          }
        }
      },
      ...
    ],
    as: "studenttopic"
  }
}

演示@Mongo Playground

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