Bhavesh Damor
Bhavesh Damor

Reputation: 81

How to get all the matching values from '$in' operator in mongoose

I have multiple checkboxes for different categories of blogs -

enter image description here

And what I want to achieve is that when I select some categories and click on filter, the specified selected blogs would be shown. I used query string parameters or URL parameters to send all the selected checkbox values to the nodejs backend -

query is an array containing all the selected categories

const response = await axios.get(`http://localhost:8000/blog/all?categories=${query}`)

And in the backend -

const cat = req.query.categories;

const all = await blogModel.find({ category: { $in: cat } });

I am passing the cat, which again consists of all the selected categories from the frontend, to $in to find all the matching blogs.

But the issue I am facing is that I am not getting matching blogs, say when I select the 'Science' checkbox and click on the filter I am able to get all the blogs having 'Science' as a category. But when I select say 'Science' & 'Food', I am getting an empty array as output even though I do have blogs of both categories in my MongoDB.

Backend console log of cat when I select 'Science' category -

enter image description here

And when I select 'Science' & 'Food' -

enter image description here

I get an empty array as output instead of all the blogs of 'Science' & 'Food' categories.

To summarize , when i select more than one categories i am getting an empty array instead of the blogs of the selected categories.And when i select only one category then i am able to get the selected blogs.It's not working for multiple categories

I am sure I am missing out on something but don't know what to google to get correct results. I also went through similar StackOverflow questions but I was not able to wrap my head around what needs to be done. I am building a simple blog website for my portfolio & thought of adding this 'filter by category' feature so that I would get to learn on how to filter values from MongoDB using mongoose. Please help me resolve this issue. Thank You.

Edit 1 - This is an example of a document from my collection enter image description here

Also 'console.log(req.query.categories)' outputs this when i select 'Science' & 'Food' from the above checkbox -

enter image description here

Upvotes: 1

Views: 1562

Answers (3)

priyanka
priyanka

Reputation: 1

const cat = req.query.categories; var splitcat = cat .split(",");

db.Posts.find({category: { $in:splitcat }})

Upvotes: 0

mohammad Naimi
mohammad Naimi

Reputation: 2359

I think you didnt path data as array to mongo query first split by , and then pass to query

const cat = req.query.categories;
cat = cat.split(",")
const all = await blogModel.find({ category: { $in: cat } });

Upvotes: 1

kumol
kumol

Reputation: 184

Yes there seems no issue in your query, make sure you are passing right data to backend like you saved "food" as category in database and searching for "Food" it will return empty array as it is case sensitive. db.Posts.find({category: { $in:["Food","Gaming"]}}) You can also try this way to fetch db.Posts.aggregate([{$match:{category: {$in:["cat","dog"]}}}])

Upvotes: 0

Related Questions