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

xwmevbvl  于 11个月前  发布在  Node.js
关注(0)|答案(1)|浏览(122)

我是MongoDB的新手,并试图从不同的集合中获取每个subjectidcompletestatus的所有主题。
$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对它们进行分组。

bpzcxfmw

bpzcxfmw1#

你的第二和第三个条件不起作用:
1.将subjectid变量与作为数组的subjectDetails.subjectid字段进行比较。
1.将topicid变量与嵌套数组subjectDetails.topicDetails.topicid字段进行比较。
因此,每个问题的解决方案:
1.使用$in运算符检查subjectDetails.subjectid数组中的subjectid变量。
1.使用$in运算符检查flatten subjectDetails.topicDetails.topicid数组中的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"
  }
}

字符串
Demo @ Mongo Playground

相关问题