MongoDB DEV · 题目 196 · 单选

Consider a people collection with the following documents: { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas" } { "_id": 2, "lastName": "Bai", "firstName": "Brian", "city": "Patna", "state": "Bihar" } { "_id": 3, "lastName": "Chang", "firstName": "Cindy", "city": "Palo Alto", "state": "California" } { "_id": 4, "lastName": "Díaz", "firstName": "Diego" } And a states collection with the following documents: { "_id": "Alabama", "motto": "Audemus jura nostra defendere" } { "_id": "Alaska", "motto": "North to the Future" } { "_id": "Arizona", "motto": "Ditat Deus" } { "_id": "Arkansas", "motto": "Regnat Populus" } { "_id": "California", "motto": "Eureka" } You perform the following aggregation against the people collection: db.people.aggregate([ { $lookup: { from: "states", localField: "state", foreignField: "_id", as: "stateInfo" } } ]) Which of the following will be the result? Choose the best answer:

题目选项

  1. A.
    { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] }
  2. B.
    { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] } { "_id": 3, "lastName": "Chang", "firstName": "Cindy", "city": "Palo Alto", "state": "California", "stateInfo": [ { "_id": "California", "motto": "Eureka" } ] } { "_id": 4, "lastName": "Díaz", "firstName": "Diego", "stateInfo": [ ] }
  3. C.
    { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] } { "_id": 3, "lastName": "Chang", "firstName": "Cindy", "city": "Palo Alto", "state": "California", "stateInfo": [ { "_id": "California", "motto": "Eureka" } ] }
  4. D.
    { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] } { "_id": 2, "lastName": "Bai", "firstName": "Brian", "city": "Patna", "state": "Bihar", "stateInfo": [ ] } { "_id": 3, "lastName": "Chang", "firstName": "Cindy", "city": "Palo Alto", "state": "California", "stateInfo": [ { "_id": "California", "motto": "Eureka" } ] } { "_id": 4, "lastName": "Díaz", "firstName": "Diego", "stateInfo": [ ] }
  5. E.
    { "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] } { "_id": 2, "lastName": "Bai", "firstName": "Brian", "city": "Patna", "state": "Bihar", "stateInfo": [ ] } { "_id": 3, "lastName": "Chang", "firstName": "Cindy", "city": "Palo Alto", "state": "California", "stateInfo": [ { "_id": "California", "motto": "Eureka" } ] }

答案置信度:高

正确答案:D

解析语言:中文

$lookup 是左外连接:people 的每条文档都会保留,匹配到的 states 文档放进 stateInfo 数组;没有 state 字段的(_id: 4)或匹配不到的(_id: 2,Bihar)其 stateInfo 为空数组。因此结果包含全部 4 条文档,只有 D 完整;A、B、C、E 都漏掉了文档。

所在套题

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

作者 · 作品 · 开源

作者与 MongoDB 作品

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

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

联系作者 ↗mlhp123@outlook.com

支持作者

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

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