我的文档包含MongoDB中近七个级别的文档,现在我需要编写一个查询来检查所有条件,例如subject=java, topic=oops, level=l1, complexity=easy, questionType=mcq
根据此要求显示集合中的所有问题。
请任何人帮助我,提前谢谢。
{
"_id" : ObjectId("59f71b4d0bec333e1707a8d3"),
"_class" : "com.wipro.domain.QuestionBank",
"subjectLists" : [
{
"subject" : "java",
"topicList" : [
{
"topic" : "oops",
"levelList" : [
{
"level" : "l1",
"complexityList" : [
{
"complexity" : "easy",
"questionTypeList" : [
{
"questionType" : "mcq",
"questionList" : [
{
"_id" : "2",
"question" : "2st question",
"options" : [
{
"a" : "1",
"b" : "2",
"c" : "3",
"d" : "4"
}
],
"correctAnswer" : "b",
"marksAlloted" : "1"
}
]
}
]
}
]
}
]
}
]
}
]
}
{
"_id" : ObjectId("59f71b700bec333e1707a8d4"),
"_class" : "com.wipro.domain.QuestionBank",
"subjectLists" : [
{
"subject" : "java",
"topicList" : [
{
"topic" : "threds",
"levelList" : [
{
"level" : "l3",
"complexityList" : [
{
"complexity" : "hard",
"questionTypeList" : [
{
"questionType" : "mcq",
"questionList" : [
{
"_id" : "3",
"question" : "3rd question",
"options" : [
{
"a" : "1",
"b" : "2",
"c" : "3",
"d" : "4"
}
],
"correctAnswer" : "b",
"marksAlloted" : "1"
}
]
}
]
}
]
}
]
}
]
}
]
}
答案 0 :(得分:0)
仅使用投影列表查询问题:
注意:通过以下查询:
db.questions.findOne({}, {'subjectLists.topicList.levelList.complexityList.questionTypeList.questionList':1})
答案 1 :(得分:0)
您可以尝试使用此脚本。
db.getCollection('document').aggregate([
{
$match:{
$and: [
{"subjectLists.subject": "java"}
,{"subjectLists.topicList.topic": "oops"}
,{"subjectLists.topicList.levelList.level": "l1"}
,{"subjectLists.topicList.levelList.complexityList.complexity": "easy"}
,{"subjectLists.topicList.levelList.complexityList.questionTypeList.questionType": "mcq"}
]
}
},
{
$unwind: "$subjectLists"
}
,{
$unwind: "$subjectLists.topicList"
}
,{
$unwind: "$subjectLists.topicList.levelList"
}
,{
$unwind: "$subjectLists.topicList.levelList.complexityList"
}
,{
$unwind: "$subjectLists.topicList.levelList.complexityList.questionTypeList"
}
,{
$unwind: "$subjectLists.topicList.levelList.complexityList.questionTypeList.questionList"
},
{
$project: {
"questionList": "$subjectLists.topicList.levelList.complexityList.questionTypeList.questionList"
}
}
,
{
$replaceRoot: { newRoot: "$questionList" }
}
])
结果:
{
"_id" : "2",
"question" : "2st question",
"options" : [
{
"a" : "1",
"b" : "2",
"c" : "3",
"d" : "4"
}
],
"correctAnswer" : "b",
"marksAlloted" : "1"
}