Reputation: 345
I have a list of products with the following details:
"products": [{
"product_name": "A",
"product_type": [{
"name": "Metal"
},
{
"name": "Wood"
},
{
"name": "Carbon"
}
],
},
{
"product_name": "B",
"product_type": [{
"name": "Metal"
},
{
"name": "Iron"
}
],
},
{
"product_name": "C",
"product_type": [{
"name": "Metal"
},
{
"name": "Wood"
}
],
},
{
"product_name": "D",
"product_type": [{
"name": "Wood"
}],
}
]
I want to filter this collection with
Product.find(query)
where sending query = ["Wood", "Carbon"] should list me products which has either Wood or Carbon type.
Or works like:
Product.find({product_type: [ { name: query } ]}) list me products with name A, C and D
Upvotes: 0
Views: 45
Reputation: 11975
You can use dot notation with $in to solve your problem:
Product.find({
"product_type.name": {
$in: ["Wood", "Carbon"]
}
})
Upvotes: 1
Reputation: 1248
You can write query like,
Product.find({product_name:{$in: [ 'A', 'C', 'D']}}) list me products with name A, C and D
Or if you want to find the documents based on product_type, then you can use following query
Product.find({
"product_type.name": {
$in: [
"Metal"
]
}
})
Refer: https://mongoplayground.net/p/vSfkpUjU494
Upvotes: 0