'Find documents that contain an id in an array field and have a name

I have following Collections:

[
 { 
   name: "apple",
   class: [620e112a03914a837dcd7585,620e112a03914a837dcd4325]
 },
 {
   name: "orange",
   class: [620e112a03914a837dcd4325,620e112a03914a837dcd7712]
 }
]

I want to execute this query:

Find all the documents which have name 'apple' and have class '620e112a03914a837dcd4325'

How are we suppose to do this in mongoose/mongodB??



Solution 1:[1]

Assuming your model is called Product, you could do it this way:

Product.find({name: 'apple', class: {"$in": ['620e112a03914a837dcd4325']}})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1