Home > Mobile >  Find documents that contain an id in an array field and have a name
Find documents that contain an id in an array field and have a name

Time:03-21

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??

CodePudding user response:

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

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