找到对象数组MongoDB中的所有元素匹配项

时间:2017-04-30 15:43:23

标签: mongodb

假设以下架构

    {
        id:"14198959",
        user_name:"kikStart2X"
        recommendations:[
                    {
                        friend_id:"1419897878",
                        friend_name:"nitpick",
                        profile_picture:"some image data",
                        game_id:"1" , 
                        game_name:"Bunny Hop"
                    },
                    {
                            friend_id:"14198848418",
                            friend_name:"applePie",
                            profile_picture:"some image data",
                            game_id:"1" , 
                            game_name:"Bunny Hop"
                    }, //etc
                ]
    }

现在我已经在互联网上搜索了很多,而且我没有找到解决方案附近的地方。我已经开始学习MongoDB所以我想我还有很长的路要走。

以下是我写的汇总

            {
                $match:
                {
                    _id:data.userId
                }
            } , 
            {
                $unwind:"$recommendations"
            }, 
            {
                $match:
                {
                    "recommendations.game_id":data.gameId
                }
            }

获得以下结果 结果:

[
    {
        friend_id:"1419897878",
        friend_name:"nitpick",
        profile_picture:"some image data",

    },
    {
      friend_id:"14198848418",
      friend_name:"applePie",
      profile_picture:"some image data",                           
     }
], // etc

但我得到了空白

data.userIddata.gameId是具有我正在搜索的用户ID和游戏ID的变量

我已将$elemMatchfind一起使用,但它工作正常但只返回一个匹配而不是多个匹配。所以我开始学习如何聚合,但没有找到运气 我做错了什么,我需要做什么。

编辑:

我的功能

getRecommendorOfGame:function(data , callback)
    {
        Players.aggregate([{
    $match: {
        id: data.userId
    }
}, {
    $project: {
        recommendations: {
            $map: {
                input: {
                    $filter: {
                        input: "$recommendations",
                        as: "resultf",
                        cond: {
                            $eq: ["$$resultf.game_id", data.gameId]
                        }
                    }
                },
                as: "resultm",
                in: {
                    friend_id: "$$resultm.friend_id",
                    friend_name: "$$resultm.friend_name",
                    profile_picture: "$$resultm.profile_picture",
                }
            }
        }
    }
}],
            function(err , res){
            if(!err)
            {
                if(res != null)
                {
                    callback(helperFunctions.returnObj("" , "RECOMMENDOR RETREIVED SUCCESSFULLTY" , {'FRIEND':res}));
                }
                else
                {
                    callback(helperFunctions.returnObj("ERROR" , "NO RECOMMENDOR FOUND FOR THIS GAME" , null));
                }
            }
            else
            {
                callback(helperFunctions.returnObj("ERROR" , err , null));
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

您不需要$unwind + $match。请改用$map + $filter

以下查询$filters recommendations数组用于匹配game_id值,然后$map投影字段以输出预期的响应。

{
    $match: {
        _id: mongoose.Types.ObjectId(data.userId)
    }
}, {
    $project: {
        recommendations: {
            $map: {
                input: {
                    $filter: {
                        input: "$recommendations",
                        as: "resultf",
                        cond: {
                            $eq: ["$$resultf.game_id", data.gameId]
                        }
                    }
                },
                as: "resultm",
                in: {
                    friend_id: "$$resultm.friend_id",
                    friend_name: "$$resultm.friend_name",
                    profile_picture: "$$resultm.profile_picture",
                }
            }
        }
    }
}

参考

https://docs.mongodb.com/manual/reference/operator/aggregation/filter/ https://docs.mongodb.com/manual/reference/operator/aggregation/map/