0

This is the database collection - Job:

{
  "skillsRequired": ["html", "css", "javascript"],
  "qualification": [ "BE", "B-Tech"],
  "company" : "microsoft"
},
{
  "skillsRequired": ["html", "ml", "ai", "python"],
  "qualification": [ "BE", "B-Tech"],
  "company" : "microsoft"
},
{
  "skillsRequired": ["ml", "ai", "python"],
  "qualification": [ "BE", "B-Tech"],
  "company" : "microsoft"
}

How can I get the following output when parameter is "skillsRequired" : ["html", "css"]

[{
  "skillsRequired": ["html", "css", "javascript"],
  "qualification": [ "BE", "B-Tech"],
  "company" : "microsoft"
},
{
  "skillsRequired": ["html", "ml", "ai", "python"],
  "qualification": [ "BE", "B-Tech"],
  "company" : "microsoft"
}]

In short, the query should return all the records/documents in which any one of the elements in parameter query matches with that of the document.

1 Answer 1

1
const query = {};

if (skillsRequiredParam && skillsRequiredParam.length > 0) 
    query.skillsRequired = {$in: skillsRequiredParam}
}     
const docs = await db.collection.find(query).exec();
Sign up to request clarification or add additional context in comments.

2 Comments

It is working as expected, but when the parameters skillsRequired array is empty, I want it to return all the documents since no conditions are provided. As of now if the parameters array is empty it is not returning any document.
Just passing {} as the query will get all the documents but as soon as put skillsRequired in there, you need to match some terms. Before the call put a check that the array is nonempty and if empty, don't add the query.

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.