Uzumaki Naruto
Uzumaki Naruto

Reputation: 753

find all element matches in object array MongoDB

suppose the following schema

    {
        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
                ]
    }

Now I have searched alot on the internet and am getting no where near the solution. I have started learning MongoDB So I guess I have a long way to go.

Following is the aggregation I wrote

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

To Get The Following Results result:

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

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

but I am getting NULL

data.userId and data.gameId are the variables that have user ID and game ID I am searching

I have used $elemMatch with find and It works fine but only returns one match and not multiple. So I started learning how to aggregate but found no luck What am I doing wrong and What Do I need to do.

EDIT:

My Function

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));
            }
        });
    }

Upvotes: 0

Views: 791

Answers (1)

s7vr
s7vr

Reputation: 75984

You don't need $unwind + $match. Use $map + $filter instead.

The below query $filters the recommendations array for matching game_id values followed by $map to project the fields to output the expected response.

{
    $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",
                }
            }
        }
    }
}

Reference

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

Upvotes: 2

Related Questions