如何在嵌套文档中获取对象x或y

时间:2018-07-20 08:12:43

标签: node.js mongodb express mongoose

我知道这已经被问过了,但是我似乎找不到答案,我想在嵌套数组中获取数据/对象。

show image problem

schedule = await Schedule.findById({_id:'5b496ec3444152122c8d839e'})
console.log(schedule.datalayout.section.data.x)

1 个答案:

答案 0 :(得分:0)

如果要在图像中获取指定的字段,则需要确定数组中的字段索引,如下所示:

console.log(schedule.datalayout.section[0].data[0].x)

此外,如果要获取数据数组中的所有x字段,则需要编写如下内容:

    for(let singleData of schedule.datalayout.section[0].data){
           console.log(singleData.x)
    }
    // for using 'of' keyword, your function must be a async function.