I want to fetch data from the MongoDB. Since I am new into Node.Js and MongoDB. I am not able to get the data.
I am passing primary category and secondary category to an API where I want to match the primary and secondary category with the array field "category" which is having two indexes 0 and 1 ,in the 0 index primary category is there and in the 1 index secondary categories are there separated by ~ .
Below is my code to get the data according to primary category but I want to get the data by matching the primary and secondary category from the db.
ProductModel
.find({ category : { $all : [req.body.primary] }})
.select('id link brand title description category image images in_stock price sale_price merchant_number part_number GroupID status promotion attributes tags currency updated -_id')
.then((productList) => {
response.status = 200;
response.msg = 'Success';
response.count = productList.length;
response.data = productList
res.json(response);
})
.catch(() => {
response.status = 500;
response.msg = 'connection error';
response.data = [];
res.json(response);
});
Sample Doc :
So I wanted to input something like ['Health','women'] & get docs where all of these elements in category array exists, kind of regex search on array elements.
Can Someone help me out to get the data from the db.

.find({ category : { $all : [req.body.primary, req.body.secondary] }})db.getCollection('AddToSet').find({$and: [{category : {$in : [/women/]}}, {category : {$in : ['Health']}}]})