MongoDB DBA · 题目 123 · 多选

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:

题目选项

  1. A.
    db.buildings.find({ "residents.age": 35 })
  2. B.
    db.buildings.find({ residents: 35 })
  3. C.
    db.buildings.find({ residents: { age: 35 } })

答案置信度:高

正确答案:A

解析语言:中文

查询数组内子文档的字段要用点号路径:{"residents.age": 35} 会在数组的任一元素上匹配 age=35,A 正确。B 拿整个数组和数字 35 比较,永远不匹配;C 的 {residents: {age: 35}} 是“某个数组元素恰好等于 {age: 35} 这个子文档”的精确匹配,而实际元素还包含 name 字段,所以也匹配不到。

所在套题

此题在题库中有 3 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗