0

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!

1 Answer 1

3

For such a complex filtering maybe it would be considerable to use POST with json body as a filter, for example:

POST http://localhost:1337/api/v1/user/search
{
  "conditions": [
    {
      "class": "skills",
      "field": "React",
      "type": "GT",
      "value": "3"
    },
    {
      "class": "skills",
      "field": "SQL",
      "type": "LT",
      "value": "3"
    },
    {
      "class": "skills",
      "field": "C++",
      "type": "EQ",
      "value": "1"
    }
  ]
}

Provided structure is, of course, not ideal, and you can modify for your cases better.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.