MongoDB DEV · Question 196 · Single choice

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:

Answer options

  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" } ] }

Answer confidence: High

Correct answer: D

Explanation language: Chinese

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

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 ↗