Pha*_*mar 5 nested nested-lists mongodb node.js
我在这里放置示例集合和预期输出。我有嵌套的模块对象数组
当我从 api 收到学生 ID、课程 ID 和模块 ID 的请求时,如果不需要将 % 发送为 0,我必须发送模块 % 如果存在
课程集
{
"_id": "courseId1",
"courseName": "course1",
"isActive": true,
"modules": [
{
"_id":"id1",
"name":"mod1",
"isActive": true
},
{
"_id":"id2",
"name":"mod2",
"isActive": true
},
{
"_id":"id3",
"name":"mod3",
"isActive": true
"modules":[
{
"_id":"id4",
"name":"mod4",
"isActive": true
},
{
"_id":"id5",
"name":"mod5",
"isActive": true,
"modules":[
{
"_id":"id6",
"name":"mod6",
"isActive": true
}
]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
课程活动合集
{
"id":"ca1",
"studentId:"std1",
"courseId:"courseId1",
mProgress:[{
"id":"ac1",
"modId":"id5",
"studentID":"std1",
"progress":20
}
{
"id":"ac2",
"modId":"id4",
"studentID":"std1",
"progress":10
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我得到 studentID="std1" , courseId="5f698ca6f5cd3551060d86e8" , moduleId="id3"
I need response link below
modules:
[
{
"modId":"id4",
"name:"mod4",
"progress":10
},
{
"modId":"id5",
"name:"mod5",
"progress":0
}
]
Run Code Online (Sandbox Code Playgroud)
我通过java脚本实现了这一点。第一次我使用 mongo 查询找到课程对象,然后使用以下函数搜索嵌套对象(即模块数组)
findNestedObj(keyToFind, valToFind,obj ) {
let foundObj;
JSON.stringify(obj , (_, nestedValue) => {
if (nestedValue && nestedValue[keyToFind] === valToFind) {
foundObj = nestedValue;
}
return nestedValue;
});
return foundObj;
}
var obj = 'here your object';
findNestedObj('_id','id3',obj );
Run Code Online (Sandbox Code Playgroud)
然后我从中计算出%
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |