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:
题目选项
- A.{ "_id": 1, "lastName": "Ansari", "firstName": "Alice", "city": "Little Rock", "state": "Arkansas", "stateInfo": [ { "_id": "Arkansas", "motto": "Regnat Populus" } ] }
- 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": [ ] }
- 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" } ] }
- 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": [ ] }
- 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 都漏掉了文档。