我是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
对它们进行分组。
1条答案
按热度按时间bpzcxfmw1#
你的第二和第三个条件不起作用:
1.将
subjectid
变量与作为数组的subjectDetails.subjectid
字段进行比较。1.将
topicid
变量与嵌套数组subjectDetails.topicDetails.topicid
字段进行比较。因此,每个问题的解决方案:
1.使用
$in
运算符检查subjectDetails.subjectid
数组中的subjectid
变量。1.使用
$in
运算符检查flattensubjectDetails.topicDetails.topicid
数组中的topicid
变量。字符串
Demo @ Mongo Playground