advanced search with multiple query in mongodb aggregation

38 views Asked by At

i have collection of 10 data collections like below

{
"name":"peter",
"movie":"spider3",
"rating":10,
"genres":["thriller","mystery"]     }

the movie controller with aggregation is

export const getMovies = async (req, res) => {
//const genre="romance"
const genre = req.query.genre
const movies = await Movie.aggregate(
    [

        { $unwind: "$genres" },
        {
            $group: {
                _id: '$genres',
                movieCount: { $sum: 1 },
                movies: {
                    $push: "$movie"
                }
            }
        },
        {$addFields:{genre:'$_id'}},
        {$project:{_id:0}},
        { $match: { genre: genre } }
    ]
)
res.status(200).json({ movies })    }

so with this I could find movies with genre. But I also need to search with rating and actor name. how to achieve it with aggregation.

my search query would be

http://localhost:4000/api/getmovie?genre=action&rating=8&name=peter

0

There are 0 answers