I'm trying to make a filtering system for my web app using MERN stack. Currently I'm able to take in the user's requests for what to filter, take in that data, see which ones the user entered (because the user can also specify "Any" in a specific category, which means it would send an empty string or array to the backend), parse that data and turn it into one final object called request which would look something like this if a user decided to select SDG 8: Decent Work & Economic Growth as the SDG, Any as the assignment type, and Economy, People as the themes:
{
sdg: 'SDG 8: Decent Work & Economic Growth',
theme: [ 'Economy', 'People' ]
}
As you can see, the assignment_type value is not there because my backend code sees that they chose Any as the value and it returned an empty string which means I don't add it to my final request object.
So now that I have this object, I want to be able to send it to the database and return the objects that contain all of this information. I want to use the $all query statement for this because I need to make sure that if a user enters 'Economy', 'People' as the themes, an object that contains the themes 'Economy', 'People', 'Technology' also gets returned because the themes the user entered exists within that.
This is what I've tried so far in my test code but it doesn't seem to be working:
var request = {
sdg: 'SDG 8: Decent Work & Economic Growth',
theme: [ 'Economy', 'People' ]
}
const test = await Project.find( {$all: {request}}).sort({ createdAt: -1 })
console.log(test)
The code above just returns all values in my database and not the one's I've tried specifying. The main reason why I'm doing it this way is because I have to filter through the requests the user made in the backend in terms of which ones the user wanted specific values for, the user could specify "Any" for all 3 categories (sdg, theme, assignment_type) which would mean that the request object would just be empty so essentially I'd just be finding every single object in the database which is exactly what I want.
I'm not sure how I could make it so that I send these requests to the backend so it only finds those objects. I would appreciate some help