如何在 mongodb 中使用数组查询数组字段?

How to query on array field with an aray in mongodb?

虚拟集合中的文档示例。

{
    "_id": "1",
    "subjects": ['English', 'Maths', 'Science', 'French',..]
}

现在我想 select 所有在数组中提到的任何主题的文档。例如:['Maths', 'French'].

所以查询的结果应该给出所有提到的任何主题的文档。

因此,对于上述示例,应返回所有具有 MathsFrench subjects 的文档。

您可以将 $in 运算符与 find()aggregate()

一起使用
db.collection.find({
  "subjects": {
    $in: [
      "English"
    ]
  }
})

或者

db.collection.aggregate([
  {
    $match: {
      subjects: {
        $in: [
          "Maths",
          "English"
        ]
      }
    }
  }
])

工作Mongo playground