I have a mongoose schema like this:
var Address = {
doorNo:String,
city:String,
state:String,
country:String,
district:String,
zipCode:String,
area:String,
locality:String
};
var StoreSchema = {
name:String,
address:Address,
category:[String],
products:[{ type:Schema.ObjectId, ref:"Product" }],
};
var ProductSchema = {
name:String,
description:String,
category:String,
subCategory:String,
store: { type:Schema.ObjectId, ref:"Store", childPath:"products" }
};
I need to filter products based on product-category and address-city. Get only those products which belong to a particular location(city in address schema) and a particular category.
I tried the following code:
Product
.find({'category':'accessories'})
.populate({
path: 'store',
match: { 'address.city': req.params.location},
select:'_id'
})
Now this returns all the products where category matches but wherever the location filter is not-satisfied it returns a store with null and returns store wherever location filter is satisfied.