嵌套在嵌套对象中,与 mongoDB 中的另一个集合具有相同的键聚合
Nested inside nested Objects with same key aggregation with another collection in mongoDB
我在这里放置示例集合和预期输出。我有嵌套的对象模块数组
当我收到 api 作为学生 ID、课程 ID 和模块 ID 的请求时,如果存在则我必须发送模块 % 如果不需要则发送 % 作为 0
Course collection
{
"_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
}
]
}
]
}
]
}
Course activity collection
{
"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 查询找到课程对象,然后我使用以下函数搜索嵌套对象(即模块数组)
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 );
然后我从中计算出 %
我在这里放置示例集合和预期输出。我有嵌套的对象模块数组
当我收到 api 作为学生 ID、课程 ID 和模块 ID 的请求时,如果存在则我必须发送模块 % 如果不需要则发送 % 作为 0
Course collection
{
"_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
}
]
}
]
}
]
}
Course activity collection
{
"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 查询找到课程对象,然后我使用以下函数搜索嵌套对象(即模块数组)
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 );
然后我从中计算出 %