在MongoDB中,如何从具有多级对象数组的集合中获取数据

8ehkhllq  于 2022-11-28  发布在  Go
关注(0)|答案(1)|浏览(158)

我刚刚开始与MongoDB在几天内,这里是我的收藏:

{
    PlayerUid:1
    ListData:
    [
        {
            Id:100
            ListInfo:[
                {
                    Uid:1,
                    content:"Those I don't care"
                },
                {
                    Uid:2,
                    content:"Those I don't care"
                },
                {
                    Uid:3,
                    content:"Those I don't care"
                },
                {
                    Uid:4,
                    content:"Those I don't care"
                }
            ]
        },
        {
            Id:101
            ListInfo:[
                {
                    Uid:5,
                    content:"Which I wanted"
                },
                {
                    Uid:6,
                    content:"Those I don't care"
                },
                {
                    Uid:7,
                    content:"Those I don't care"
                },
                {
                    Uid:8,
                    content:"Those I don't care"
                }
            ]
        }
    ]
},
{
    PlayerUid:2
    ListData:
    [
        {
            Id:100
            ListInfo:[
                {
                    Uid:9,
                    content:"Those I don't care"
                },
                {
                    Uid:10,
                    content:"Those I don't care"
                },
                {
                    Uid:11,
                    content:"Those I don't care"
                },
                {
                    Uid:12,
                    content:"Those I don't care"
                }
            ]
        },
        {
            Id:101
            ListInfo:[
                {
                    Uid:13,
                    content:"Those I don't care"
                },
                {
                    Uid:14,
                    content:"Those I don't care"
                },
                {
                    Uid:15,
                    content:"Those I don't care"
                },
                {
                    Uid:16,
                    content:"Those I don't care"
                }
            ]
        }
    ]
}

我想获取条件为PlayerUid = 1,Id:101,Uid = 5的数据。
我试过了

findOne({ PlayerUid: 1}, { ListData: { $elemMatch: { Id: 101 } } })

我得到了

{
  ListData: [ { Id: 101, ListInfo: [Array] } ]
}

然后我就被卡住了,我不知道如何在ListInfo中获取或归档数据。

{
    Uid:5,
    content:"Which I wanted"
}
or
{
    PlayerUid:1
    ListData:
    [
        {
            Id:100
            ListInfo:
            [
                {
                    Uid:5,
                    content:"Which I wanted"
                }
            ]
        }
    ]
}

可能吗?
这是我的第一篇文章,为我在这里做错的事情道歉。

rt4zxlrg

rt4zxlrg1#

我已经将store构造函数更改为key-value,如

{
    '1':
    {
        '100':
        {
            '9':{
                Uid:9,
                content:"Those I don't care"
            },
            '10':{
                Uid:10,
                content:"Those I don't care"
            }
        }
        '200':{
            '11':{
                Uid:11,
                content:"Those I don't care"
            },
            '12':{
                Uid:12,
                content:"That I wanted"
            }
        }
    },
    '2':
    {
        '100':
        {
            '13':{
                Uid:13,
                content:"Those I don't care"
            },
            '14':{
                Uid:14,
                content:"Those I don't care"
            }
        }
        '200':{
            '15':{
                Uid:15,
                content:"Those I don't care"
            },
            '16':{
                Uid:16,
                content:"That I wanted"
            }
        }
    }
}

一切都很顺利。

相关问题