gpt4 book ai didi

node.js - 嵌套在具有相同键聚合的嵌套对象中,与 mongoDB 中的另一个集合

转载 作者:行者123 更新时间:2023-12-03 12:13:01 24 4
gpt4 key购买 nike

在这里,我放置示例集合和预期输出。我有嵌套的模块数组对象
当我从 api 收到作为学生 ID、类(class) 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脚本实现了这一点。第一次我使用 mongo 查询找到类(class)对象,然后我使用以下函数搜索嵌套对象(即模块数组)

    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 );
然后我从中计算出 %

关于node.js - 嵌套在具有相同键聚合的嵌套对象中,与 mongoDB 中的另一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64275709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com