查找在数组字段中包含 id 并具有名称的文档

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

我有以下合集:

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

我想执行这个查询:

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

我们应该如何在 mongoose/mongodB 中做到这一点??

假设您的模型名为 Product,您可以这样做:

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