【问题标题】:retrieving only required element from child objects Mongodb从子对象 Mongodb 中仅检索所需的元素
【发布时间】:2020-03-16 03:34:15
【问题描述】:

我有一个文件

{
owner:[
    {
        '_id':ObjectId(''),
        'userName':'sasasa',
        'name':"Sasa Sa"
    },
    ....
    ....
],
team:[
    {
        'name':'team 1',
        'member':[
            'sasasa',
            'aarwalka',
            'john'
        ]
    }
],
matches:[{
    'id':ObjectId(),
    'name':'cricket',
    'players':[
        {
            '_id':ObjectId(''),
            'userName':'sasasa',
            'name':"Sasa Sa"
        },
        .....
        .....
    ],
    'sponsors':[
        {
            '_id':ObjectId(''),
            'userName':'sasasa',
            'name':"Sasa Sa"    
        },
        .....
        .....
    ]
}]}

我需要查询所有用户名,即特定比赛的所有者用户名、团队成员、球员和赞助商(基于 _id)。

我尝试通过$project & $aggregation 实现,$filter 但是无法为玩家和赞助商安排用户名。很高兴有任何建议来实现这一目标。

【问题讨论】:

  • 你能详细说明你想做什么吗?
  • 实际上我想要一个与匹配相关的用户名数组,例如 [ sasasa, aarwalka, john,...]

标签: mongodb mongoose nosql


【解决方案1】:

好的,如果我根据特定文档 _id 理解正确,您想要与文档相关联的人的姓名。

我们所要做的就是逐步将名称分组

db.collection.aggregate(
    {
        $match: {
            // your initial match here
        }
    },
    // starting with getting owners.
    {
        $unwind: {
                path: "$owner",
                preserveNullAndEmptyArrays: true
            }
    },
    {
        $group: {
            _id: "all",
            owners: {$addToSet: "$owner.userName"},
            team: {$first: "$team"},
            matches: {$first: "$matches"},
        }
    },
    // just repeat same two steps for the others
    {
        $unwind: {
                path: "$team",
                preserveNullAndEmptyArrays: true
            }
    },
    {
        $unwind: {
                path: "$team.member",
                preserveNullAndEmptyArrays: true
            }
    },
    {
        $group: {
                _id: "all",
                owners: {$first: "$owners"},
                teams: {$addToSet: "$team.member"},
                matches: {$first: "$matches"},
            }
    },
    {
        $unwind: {
                path: "$matches",
                preserveNullAndEmptyArrays: true
            }
    },
    {
        $unwind: {
                path: "$matches.players",
                preserveNullAndEmptyArrays: true
            }
    },
    {
        $group: {
                _id: "all",
                owners: {$first: "$owners"},
                teams: {$first: "$teams"},
                matches: {$addToSet: "$matches.players.userName"},
                sponsors: {$first: "$matches.sponsors"}
            }
    },
    {
        $unwind: {
            path: "$sponsors",
            preserveNullAndEmptyArrays: true
        }
    },

    {
        $group: {
            _id: "all",
            owners: {$first: "$owners"},
            teams: {$first: "$teams"},
            matches: {$first: "$matches"},
            sponsors: {$addToSet: "$sponsors.userName"}
        }
    },
    // finally just concat all the arrays.
    {
        $project: {
            result: { $setUnion: [ "$owners", "$teams",  "$matches", "$sponsors"] }

        }
    }
])
  • 您可以在每个数组的投影阶段使用$reduce 更“优雅地”编写此聚合,但我觉得为了可读性,我们一步一步进行会更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多