MongoDB获取项目数组中的item.field列表

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

我昨天发布了一个问题-因为,如果不知道如何“询问”,那么它的呈现方式很差-因此,如果您识别出结构/数据,请重新阅读,希望我能够以清晰的方式呈现它方式。

我想从多个嵌套对象数组中返回一个项目数组。

我的最终目标是看起来像这样...

{person:1, cost: ['23,543','56,556','8,500']},
{person:2, cost: []}

[这使我知道2条信息,1-它使我可以对两个或更多对象数组进行总计,2-它使我可以看到尚未填写信息的人。

场景:

我有几个对象,我想确定人们是否在填充对象的'成本'。

所以模型看起来像这样。

person : {
    my_cars: [
        {
            make:
            model:
            cost:
        }
    ],
    my_motorcycles: [
        {
            make:
            model:
            cost:
        }
    ]
}

一些数据看起来像这样(这个数据对应于我上面想要的结果...)

person : {
    _id: 1
    ,my_cars: [
        {
            make: 'ford'
            model: 'taurus'
            cost: null
        },
        {
            make: 'ford'
            model: 'mustang'
            cost:'23,543'
        }
        ,{
            make: 'lincoln'
            model: 'navigator'
            cost: '56,556'
        }
    ]
    ,my_motorcycles: [
        {
            make: 'ducati'
            model: ''
            cost: '8,500'
        }
        ,{
            make: 'yamaha'
            model: 'v-star 650'
            cost: ''
        }
    ]
}
,person : {
    _id: 2
    ,my_cars: [
        {
            make: 'ford'
            model: 'taurus'
            cost: null
        },
        ,{
            make: 'chevy'
            model: 'chevette'
            cost: null
        }
    ]
    ,my_motorcycles: [
        {
            make: 'harley-davidson'
            model: 'softtail'
            cost: ''
        }
        ,{
            make: 'honda'
            model: 'x650'
            cost: ''
        }
    ]
}

所以我想做的是放松但是如果我尝试

$unwind{
    path: "$my_cars.cost"
}
,$unwind{
    path: "$my_motorcycles.cost"
}

这不起作用...

非常感谢在接近一个输出的帮助中了解多个嵌套数组中字段的实例号

mongodb aggregation-framework
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.