MongoDB DBA · Question 123 · Multiple choice

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

  1. A.
    db.buildings.find({ "residents.age": 35 })
  2. B.
    db.buildings.find({ residents: 35 })
  3. 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 字段,所以也匹配不到。

Papers containing this question

This question appears in 3 bank records. Public pages combine identical prompts and options. Selected questions display the highest-confidence record; the others show a short preview. Paper-based learning preserves each record's answer, explanation, and confidence.

AUTHOR · PROJECTS · OPEN SOURCE

Author & MongoDB projects

Building data platforms, MongoDB migration and sync tools, and cloud operations products.

Contact the author for any MongoDB needs.

Contact the author ↗mlhp123@outlook.com

Support the author

If this question bank helps you, optional donations are welcome.

Scan with Alipay to support the authorOpen full-size donation image ↗