I am creating a MERN app that will allow me to query my database for users with skills. My issue is I am hitting an API endpoint to search for these users in my mongoDB database, and I'm not sure how to represent these queries in a URL params string.
If i want to find users who have React skills greaters than 3 years experience, and have SQL skills with greater less than 5 years, and have C++ skills with 1 year experience, here is the query for mongoDB:
User.find({
$and: [
{ skills: { $elemMatch: { skill: 'React', yearsExperience: { $gt: 3 } } } },
{
skills: { $elemMatch: { skill: 'SQL', yearsExperience: { $lt: 5 } } },
},
{
skills: { $elemMatch: { skill: 'C++', yearsExperience: 1 } },
}
],
})
Here is how that data for the user is stored in the database:
skills: [
{skill: 'React', yearsExperience: 3},
{skill: 'HTML', yearsExperience: 5},
{skill: 'JavaScript', yearsExperience: 5},
{skill: 'SQL', yearsExperience: 3},
{skill: 'TypeScript', yearsExperience: 1},
{skill: 'C++', yearsExperience: 1}
]
I am trying to convert the first query into a url to pass to my API to get the relevant users but I am unsure how to represent all that info in params. I have tried with no luck:
http://localhost:1337/api/v1/user?skills=React,[gt]3&skills=SQL,[lt]5&skills=C++,1
Any help would be greatly appreciated!