收藏
回答

关于云开发数据库 嵌套数组的聚合查询问题


Store 文档结构是这样的

{
    _id"store01",
    orderHistory:[
        'order01',
        'order02',
        'order03'
    ]
}


Order 文档结构

{
    _id:'order01',
    products:[
        {id'item01', quantity:20},
        {id'item02', quantity:10},
        {id'item03', quantity:20},
        {id'item04', quantity:5},
    ],
    buyer:'uid02'
},
{
    _id:'order02',
    products:[
        {id'item02', quantity:10},
        {id'item03', quantity:20},
    ],
    buyer:'uid01'
}


Item 文档结构

{
    _id:'item01',
    name:'菠萝'
},
{
    _id:'item02',
    name:'芒果'
},
{
    _id:'item03',
    name:'西瓜'
},
{
    _id:'item04',
    name:'番茄'
}


User文档结构

{
    _id:'uid01',
    name:'user Bob',
},
{
    _id:'uid02',
    name:'Alice',
}


问题: 我现在想将 Store 的orderHsttoryId映射成具体的文档。 而这些文档内,又会有 ItemId组成的数组和userId, 我想把这些也查询出来形成类似这样的结构(可以有不一样,如果有更好的方式也欢迎指出来)是否能用一个聚合把这些Id全部映射成具体的文档呢?

{
    _id"store01",
    orderHistory:[
        {
            _id:'order01',
            products:[
                {
                    item:{_id:'item01',name:'菠萝'},
                    quantity:20
                },
                {
                    item:{id'item02',name:'芒果'},
                    quantity:10
                },
                {
                    item:{id'item03',name:'西瓜'},
                    quantity:10
                },
                {
                    item:{id'item04',name:'番茄'},
                    quantity:5
                },
            ],
            buyer:{
                _id:'user02'
                name:'Alice'
            }
        },
        {
            _id:'order02',
            products:[
               ...
            ],
            buyer:...
        },
        ...
        ...,
    ]
}
最后一次编辑于  2020-09-17
回答关注问题邀请回答
收藏
登录 后发表内容
问题标签