7. Consider a collection called buildings where every document contains the following fields: { "_id": { "address": "123 Example Street" }, "type": "residential", "residents": [ { "name": "Ananya", "age": 35 }, { "name": "Yash", "age": 32 } ] } Which of the following queries will find all documents in the buildings collection where at least one of the residents subdocuments has age equal to 35? Check all that apply:
Answer options
- A.db.buildings.find({ "residents.age": 35 })
- B.db.buildings.find({ residents: 35 })
- C.db.buildings.find({ residents: { age: 35 } })
Answer confidence: High
Correct answer: A
Explanation language: Chinese
查询数组内子文档的字段要用点号路径:{"residents.age": 35} 会在数组的任一元素上匹配 age=35,A 正确。B 拿整个数组和数字 35 比较,永远不匹配;C 的 {residents: {age: 35}} 是“某个数组元素恰好等于 {age: 35} 这个子文档”的精确匹配,而实际元素还包含 name 字段,所以也匹配不到。