[英]Nested inside nested Objects with same key aggregation with another collection in mongoDB
我在这里放置示例 collections 和预期的 output。我有对象的嵌套模块数组
当我从 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
}
]
}
]
}
]
}
课程活动合集
{
"id":"ca1",
"studentId:"std1",
"courseId:"courseId1",
mProgress:[{
"id":"ac1",
"modId":"id5",
"studentID":"std1",
"progress":20
}
{
"id":"ac2",
"modId":"id4",
"studentID":"std1",
"progress":10
}
]
}
如果我得到 studentID="std1", courseId="5f698ca6f5cd3551060d86e8", moduleId="id3"
I need response link below
modules:
[
{
"modId":"id4",
"name:"mod4",
"progress":10
},
{
"modId":"id5",
"name:"mod5",
"progress":0
}
]
我通过 java 脚本实现了这一点。 1st 我使用 mongo 查询找到课程 object 之后我使用以下 function 搜索嵌套对象(即模块数组)
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 );
然后我从中计算了 %
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.