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

问题描述 投票:0回答:1

我知道之前已经问过,但我似乎无法找到答案,我想在嵌套数组中获取数据/对象。

show image problem

schedule = await Schedule.findById({_id:'5b496ec3444152122c8d839e'})
console.log(schedule.datalayout.section.data.x)
node.js mongodb express mongoose
1个回答
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. 
© www.soinside.com 2019 - 2024. All rights reserved.